secrez 1.1.0 → 1.1.2

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.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +414 -291
  3. package/bin/secrez.js +50 -47
  4. package/coverage.report +81 -0
  5. package/package.json +18 -20
  6. package/src/Command.js +78 -57
  7. package/src/PreCommand.js +74 -70
  8. package/src/Welcome.js +144 -134
  9. package/src/cliConfig.js +14 -14
  10. package/src/commands/Alias.js +123 -100
  11. package/src/commands/Bash.js +10 -12
  12. package/src/commands/Cat.js +117 -107
  13. package/src/commands/Cd.js +39 -42
  14. package/src/commands/Chat.js +75 -63
  15. package/src/commands/Conf.js +123 -99
  16. package/src/commands/Contacts.js +189 -171
  17. package/src/commands/Copy.js +132 -113
  18. package/src/commands/Courier.js +123 -105
  19. package/src/commands/Ds.js +88 -76
  20. package/src/commands/Edit.js +122 -103
  21. package/src/commands/Export.js +153 -114
  22. package/src/commands/Find.js +115 -110
  23. package/src/commands/Help.js +20 -23
  24. package/src/commands/Import.js +296 -225
  25. package/src/commands/Lcat.js +36 -39
  26. package/src/commands/Lcd.js +38 -39
  27. package/src/commands/Lls.js +58 -55
  28. package/src/commands/Lpwd.js +20 -24
  29. package/src/commands/Ls.js +107 -97
  30. package/src/commands/Mkdir.js +35 -38
  31. package/src/commands/Mv.js +147 -114
  32. package/src/commands/Paste.js +68 -65
  33. package/src/commands/Pwd.js +18 -23
  34. package/src/commands/Quit.js +22 -24
  35. package/src/commands/Rm.js +78 -70
  36. package/src/commands/Shell.js +31 -32
  37. package/src/commands/Ssh.js +77 -63
  38. package/src/commands/Tag.js +133 -112
  39. package/src/commands/Totp.js +166 -136
  40. package/src/commands/Touch.js +169 -56
  41. package/src/commands/Use.js +44 -41
  42. package/src/commands/Ver.js +16 -18
  43. package/src/commands/Whoami.js +34 -37
  44. package/src/commands/chat/Contacts.js +41 -44
  45. package/src/commands/chat/Help.js +20 -23
  46. package/src/commands/chat/Join.js +59 -55
  47. package/src/commands/chat/Leave.js +16 -22
  48. package/src/commands/chat/Quit.js +19 -24
  49. package/src/commands/chat/Send.js +58 -57
  50. package/src/commands/chat/Show.js +60 -51
  51. package/src/commands/chat/Whoami.js +18 -22
  52. package/src/commands/index.js +20 -22
  53. package/src/index.js +3 -3
  54. package/src/prompts/ChatPrompt.js +87 -82
  55. package/src/prompts/ChatPromptMock.js +11 -17
  56. package/src/prompts/CommandPrompt.js +146 -138
  57. package/src/prompts/Completion.js +64 -69
  58. package/src/prompts/MainPrompt.js +84 -77
  59. package/src/prompts/MainPromptMock.js +19 -30
  60. package/src/prompts/MultiEditorPrompt.js +21 -22
  61. package/src/prompts/SigintManager.js +21 -24
  62. package/src/utils/AliasManager.js +16 -18
  63. package/src/utils/ContactManager.js +15 -17
  64. package/src/utils/Fido2Client.js +59 -49
  65. package/src/utils/HelpProto.js +130 -117
  66. package/src/utils/Logger.js +48 -50
  67. package/.eslintignore +0 -0
  68. package/.eslintrc +0 -33
  69. package/.jshintrc +0 -3
@@ -1,103 +1,112 @@
1
- const {UglyDate} = require('@secrez/utils')
2
-
3
- class Show extends require('../../Command') {
1
+ const { UglyDate } = require("@secrez/utils");
4
2
 
3
+ class Show extends require("../../Command") {
5
4
  setHelpAndCompletion() {
6
5
  this.cliConfig.chatCompletion.show = {
7
6
  _func: this.selfCompletion(this),
8
- _self: this
9
- }
10
- this.cliConfig.chatCompletion.help.show = true
7
+ _self: this,
8
+ };
9
+ this.cliConfig.chatCompletion.help.show = true;
11
10
  this.optionDefinitions = [
12
11
  {
13
- name: 'help',
14
- alias: 'h',
15
- type: Boolean
12
+ name: "help",
13
+ alias: "h",
14
+ type: Boolean,
16
15
  },
17
16
  {
18
- name: 'from',
19
- alias: 'f',
17
+ name: "from",
18
+ alias: "f",
20
19
  type: String,
21
- multiple: true
20
+ multiple: true,
22
21
  },
23
22
  {
24
- name: 'to',
25
- alias: 't',
23
+ name: "to",
24
+ alias: "t",
26
25
  type: String,
27
- multiple: true
26
+ multiple: true,
28
27
  },
29
28
  {
30
- name: 'verbose',
31
- type: Boolean
32
- }
33
- ]
29
+ name: "verbose",
30
+ type: Boolean,
31
+ },
32
+ ];
34
33
  }
35
34
 
36
35
  help() {
37
36
  return {
38
- description: ['Show chat history in a room'],
37
+ description: ["Show chat history in a room"],
39
38
  examples: [
40
- ['show -f 2 days -t an hour', 'after joining someone, shows all the messages from 2 days ago to 1 hour ago'],
41
- ['show', 'in the joined context, shows all the messages'],
42
- ['show -f 1596129722352', 'shows all the messages since the timestamp'],
43
- ['show -f 2020-07-06 -t 1 hour ago', 'shows all the messages since the 6th of July to an hour ago'],
44
- ['show -f 3d -t 1h', 'shows all the messages since 3 days ago to an hour ago'],
45
- ['show -f 1M', 'shows all the messages since a month ago (30 days)'],
46
- ['show -f 2h --verbose', 'shows the messages with exact dates and times']
47
- ]
48
- }
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
+ };
49
60
  }
50
61
 
51
62
  getTimestamp(date, param) {
52
63
  if (!this.uglyDate) {
53
- this.uglyDate = new UglyDate()
64
+ this.uglyDate = new UglyDate();
54
65
  }
55
- let timestamp
66
+ let timestamp;
56
67
  try {
57
- timestamp = this.uglyDate.uglify(date)
58
- } catch(e) {
68
+ timestamp = this.uglyDate.uglify(date);
69
+ } catch (e) {
59
70
  if (/^[0-9]+$/.test(date)) {
60
- timestamp = parseInt(date)
71
+ timestamp = parseInt(date);
61
72
  }
62
73
  if (!timestamp || isNaN(timestamp)) {
63
74
  try {
64
- let d = new Date(date)
65
- timestamp = d.getUTCDate().getTime()
66
- } catch(e) {
67
- throw new Error(`Bad or unsupported date format for "--${param}"`)
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}"`);
68
79
  }
69
80
  }
70
81
  }
71
- return timestamp
82
+ return timestamp;
72
83
  }
73
84
 
74
85
  async show(options) {
75
86
  if (!this.prompt.environment.room) {
76
- throw new Error('You must join a conversation to use "show"')
87
+ throw new Error('You must join a conversation to use "show"');
77
88
  }
78
89
  if (options.from) {
79
- options.minTimestamp = this.getTimestamp(options.from.join(' '), 'from')
90
+ options.minTimestamp = this.getTimestamp(options.from.join(" "), "from");
80
91
  }
81
92
  if (options.to) {
82
- options.maxTimestamp = this.getTimestamp(options.to.join(' '), 'to')
93
+ options.maxTimestamp = this.getTimestamp(options.to.join(" "), "to");
83
94
  }
84
- return this.prompt.environment.readHistoryMessages(options)
95
+ return this.prompt.environment.readHistoryMessages(options);
85
96
  }
86
97
 
87
98
  async exec(options = {}) {
88
99
  if (options.help) {
89
- return this.showHelp()
100
+ return this.showHelp();
90
101
  }
91
102
  try {
92
- this.validate(options)
93
- await this.show(options)
103
+ this.validate(options);
104
+ await this.show(options);
94
105
  } catch (e) {
95
- this.Logger.red(e.message)
106
+ this.Logger.red(e.message);
96
107
  }
97
- await this.prompt.run()
108
+ await this.prompt.run();
98
109
  }
99
110
  }
100
111
 
101
- module.exports = Show
102
-
103
-
112
+ module.exports = Show;
@@ -1,47 +1,43 @@
1
-
2
- class Whoami extends require('../../Command') {
3
-
1
+ class Whoami extends require("../../Command") {
4
2
  setHelpAndCompletion() {
5
3
  this.cliConfig.chatCompletion.whoami = {
6
4
  _func: this.selfCompletion(this),
7
- _self: this
8
- }
9
- this.cliConfig.chatCompletion.help.whoami = true
5
+ _self: this,
6
+ };
7
+ this.cliConfig.chatCompletion.help.whoami = true;
10
8
  this.optionDefinitions = [
11
9
  {
12
- name: 'help',
13
- alias: 'h',
14
- type: Boolean
15
- }
16
- ]
10
+ name: "help",
11
+ alias: "h",
12
+ type: Boolean,
13
+ },
14
+ ];
17
15
  }
18
16
 
19
17
  help() {
20
- return this.prompt.environment.prompt.commands.whoami.help()
18
+ return this.prompt.environment.prompt.commands.whoami.help();
21
19
  }
22
20
 
23
21
  async customCompletion(options, originalLine, defaultOption) {
24
- return []
22
+ return [];
25
23
  }
26
24
 
27
25
  async whoami(options) {
28
- return await this.prompt.environment.prompt.commands.whoami.whoami(options)
26
+ return await this.prompt.environment.prompt.commands.whoami.whoami(options);
29
27
  }
30
28
 
31
29
  async exec(options = {}) {
32
30
  if (options.help) {
33
- return this.showHelp()
31
+ return this.showHelp();
34
32
  }
35
33
  try {
36
- this.validate(options)
37
- await this.whoami(options)
34
+ this.validate(options);
35
+ await this.whoami(options);
38
36
  } catch (e) {
39
- this.Logger.red(e.message)
37
+ this.Logger.red(e.message);
40
38
  }
41
- await this.prompt.run()
39
+ await this.prompt.run();
42
40
  }
43
41
  }
44
42
 
45
- module.exports = Whoami
46
-
47
-
43
+ module.exports = Whoami;
@@ -1,44 +1,42 @@
1
- const fs = require('fs-extra')
2
- const _ = require('lodash')
1
+ const fs = require("fs-extra");
2
+ const _ = require("lodash");
3
3
 
4
4
  class Commands {
5
-
6
5
  constructor(prompt, config, subfolder) {
7
- this.prompt = prompt
8
- this.config = config
9
- this.subfolder = subfolder
6
+ this.prompt = prompt;
7
+ this.config = config;
8
+ this.subfolder = subfolder;
10
9
  }
11
10
 
12
11
  getCommands(exceptions = [], refresh) {
13
12
  if (!this.list || refresh) {
14
- let folder = __dirname
13
+ let folder = __dirname;
15
14
  if (this.subfolder) {
16
- folder += '/' + this.subfolder
15
+ folder += "/" + this.subfolder;
17
16
  }
18
- this.list = _.filter(fs.readdirSync(folder), e => e !== 'index.js')
19
- this.commands = {}
17
+ this.list = _.filter(fs.readdirSync(folder), (e) => e !== "index.js");
18
+ this.commands = {};
20
19
  for (let file of this.list) {
21
- let command = file.split('.')[0]
20
+ let command = file.split(".")[0];
22
21
  if (exceptions.includes(command)) {
23
- continue
22
+ continue;
24
23
  }
25
24
  try {
26
- const klass = require(`${folder}/${command}`)
27
- const instance = new klass(this.prompt, this.config)
28
- instance.setHelpAndCompletion()
29
- this.commands[command.toLowerCase()] = instance
25
+ const klass = require(`${folder}/${command}`);
26
+ const instance = new klass(this.prompt, this.config);
27
+ instance.setHelpAndCompletion();
28
+ this.commands[command.toLowerCase()] = instance;
30
29
  } catch (e) {
31
30
  /* istanbul ignore if */
32
- if (process.env.NODE_ENV === 'dev') {
33
- console.error(e)
34
- console.debug(`${file} is not a command`)
31
+ if (process.env.NODE_ENV === "dev") {
32
+ console.error(e);
33
+ console.debug(`${file} is not a command`);
35
34
  }
36
35
  }
37
36
  }
38
37
  }
39
- return this.commands
38
+ return this.commands;
40
39
  }
41
-
42
40
  }
43
41
 
44
- module.exports = Commands
42
+ module.exports = Commands;
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  module.exports = {
2
- version: require('../package').version,
3
- Prompt: require('./prompts/MainPromptMock')
4
- }
2
+ version: require("../package").version,
3
+ Prompt: require("./prompts/MainPromptMock"),
4
+ };
@@ -1,162 +1,167 @@
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') {
1
+ const chalk = require("chalk");
2
+ const cliConfig = require("../cliConfig");
3
+ const Commands = require("../commands");
4
+ const { sleep, UglyDate, decolorize, getCols } = require("@secrez/utils");
7
5
 
6
+ class ChatPrompt extends require("./CommandPrompt") {
8
7
  async init(options) {
9
- this.secrez = options.secrez
8
+ this.secrez = options.secrez;
10
9
  this.getReady({
11
10
  historyPath: options.historyPath,
12
- completion: 'chatCompletion',
13
- commands: (new Commands(this, cliConfig, 'chat')).getCommands(),
11
+ completion: "chatCompletion",
12
+ commands: new Commands(this, cliConfig, "chat").getCommands(),
14
13
  environment: options.environment,
15
- context: 'chat'
16
- })
17
- await this.loadSavedHistory()
18
- this.uglyDate = new UglyDate
14
+ context: "chat",
15
+ });
16
+ await this.loadSavedHistory();
17
+ this.uglyDate = new UglyDate();
19
18
  }
20
19
 
21
20
  async start() {
22
- this.stop = false
23
- for (; ;) {
24
- await sleep(1000)
21
+ this.stop = false;
22
+ for (;;) {
23
+ await sleep(1000);
25
24
  if (this.stop) {
26
- break
25
+ break;
27
26
  }
28
27
  if (!this.environment.room || this.skip) {
29
- continue
28
+ continue;
30
29
  }
31
30
  try {
32
- let newMessages = await this.environment.courier.getRecentMessages({direction: 1})
31
+ let newMessages = await this.environment.courier.getRecentMessages({
32
+ direction: 1,
33
+ });
33
34
  if (newMessages.length) {
34
- this.onMessages(newMessages)
35
+ this.onMessages(newMessages);
35
36
  }
36
37
  } catch (e) {
37
38
  // console.log(e)
38
- break
39
+ break;
39
40
  }
40
41
  }
41
42
  }
42
43
 
43
44
  prePromptMessage(options = {}) {
44
45
  if (this.environment.room) {
45
- let nicks = this.nicks(this.environment.room[0].contact)
46
+ let nicks = this.nicks(this.environment.room[0].contact);
46
47
  return [
47
- chalk.grey(' ' + nicks[0]),
48
+ chalk.grey(" " + nicks[0]),
48
49
  // chalk.bold(this.environment.room[0].contact)
49
- ].join('')
50
+ ].join("");
50
51
  } else {
51
- return chalk.reset('Secrez/chat')
52
+ return chalk.reset("Secrez/chat");
52
53
  }
53
54
  }
54
55
 
55
56
  promptMessage() {
56
57
  if (this.environment.room) {
57
- return '>'
58
+ return ">";
58
59
  } else {
59
- return '$'
60
+ return "$";
60
61
  }
61
62
  }
62
63
 
63
- nicks(name = '') {
64
- let max = Math.max(2, name.length)
64
+ nicks(name = "") {
65
+ let max = Math.max(2, name.length);
65
66
  let result = [
66
- 'me' + ' '.repeat(max - 2),
67
- name + ' '.repeat(max - name.length)
68
- ]
69
- return result
67
+ "me" + " ".repeat(max - 2),
68
+ name + " ".repeat(max - name.length),
69
+ ];
70
+ return result;
70
71
  }
71
72
 
72
73
  async onMessages(messages, options = {}) {
73
- delete this.prefixLength
74
- let rl = this.getRl()
75
- let position = rl.cursor
76
- let presetLine = rl.line
77
- let diff = (presetLine.length - position)
78
- process.stdout.clearLine()
79
- process.stdout.cursorTo(0)
74
+ delete this.prefixLength;
75
+ let rl = this.getRl();
76
+ let position = rl.cursor;
77
+ let presetLine = rl.line;
78
+ let diff = presetLine.length - position;
79
+ process.stdout.clearLine();
80
+ process.stdout.cursorTo(0);
80
81
  for (let message of messages) {
81
- process.stdout.write(this.formatResults(message, options) + '\n')
82
+ process.stdout.write(this.formatResults(message, options) + "\n");
82
83
  }
83
84
  if (options.lastLine) {
84
- process.stdout.write(options.lastLine + '\n')
85
+ process.stdout.write(options.lastLine + "\n");
85
86
  }
86
87
  if (!options.fromHistory) {
87
- process.stdout.write(this.lastPrefix + ' ' + chalk.bold('>') + ' ')
88
+ process.stdout.write(this.lastPrefix + " " + chalk.bold(">") + " ");
88
89
  }
89
- process.stdout.write(presetLine)
90
- process.stdout.moveCursor(-diff)
91
- rl.line = presetLine.substring(0, presetLine.length - diff)
92
- rl.write(null, {ctrl: true, name: 'e'})
93
- rl.line = presetLine
90
+ process.stdout.write(presetLine);
91
+ process.stdout.moveCursor(-diff);
92
+ rl.line = presetLine.substring(0, presetLine.length - diff);
93
+ rl.write(null, { ctrl: true, name: "e" });
94
+ rl.line = presetLine;
94
95
  }
95
96
 
96
97
  formatSpaces(message, prefix) {
97
98
  if (!this.prefixLength) {
98
- this.prefixLength = decolorize(prefix, true).length
99
- this.cols = getCols()
99
+ this.prefixLength = decolorize(prefix, true).length;
100
+ this.cols = getCols();
100
101
  }
101
- let cols = this.cols - this.prefixLength
102
- let rows = []
102
+ let cols = this.cols - this.prefixLength;
103
+ let rows = [];
103
104
 
104
105
  for (;;) {
105
- let partial = message.substring(0, cols + 1)
106
- let lastIndex = partial.lastIndexOf(' ')
106
+ let partial = message.substring(0, cols + 1);
107
+ let lastIndex = partial.lastIndexOf(" ");
107
108
  rows.push(
108
- (rows.length ? ' '.repeat(this.prefixLength - 1) : '') +
109
+ (rows.length ? " ".repeat(this.prefixLength - 1) : "") +
109
110
  (message.length < cols ? message : message.substring(0, lastIndex))
110
- )
111
+ );
111
112
  if (message.length < cols) {
112
- break
113
+ break;
113
114
  } else {
114
- message = message.substring(lastIndex)
115
+ message = message.substring(lastIndex);
115
116
  }
116
117
  }
117
- return rows.join('\n')
118
+ return rows.join("\n");
118
119
  }
119
120
 
120
121
  formatResults(message, options) {
121
- let from = message.direction === 1
122
- let contact = this.environment.contactsByPublicKey[message.publickey]
123
- let nicks = this.nicks(contact)
124
- let time = ''
122
+ let from = message.direction === 1;
123
+ let contact = this.environment.contactsByPublicKey[message.publickey];
124
+ let nicks = this.nicks(contact);
125
+ let time = "";
125
126
  if (options.fromHistory) {
126
127
  if (options.verbose) {
127
- time = new Date(message.timestamp).toISOString()
128
+ time = new Date(message.timestamp).toISOString();
128
129
  } else {
129
- time = this.uglyDate.shortify(message.timestamp)
130
+ time = this.uglyDate.shortify(message.timestamp);
130
131
  if (time.length < 3) {
131
- time = ' ' + time
132
+ time = " " + time;
132
133
  }
133
134
  }
134
135
  }
135
136
  let prefix = [
136
- chalk.grey(from ? '@' : ' ' + nicks[0]),
137
- chalk.bold(from ? nicks[1] : ''),
138
- chalk.grey(time ? ' ' + time : ''),
139
- chalk.bold(' > '),
140
- ].join('')
141
- return prefix + chalk[from ? 'reset' : 'grey'](this.formatSpaces(message.decrypted, prefix))
137
+ chalk.grey(from ? "@" : " " + nicks[0]),
138
+ chalk.bold(from ? nicks[1] : ""),
139
+ chalk.grey(time ? " " + time : ""),
140
+ chalk.bold(" > "),
141
+ ].join("");
142
+ return (
143
+ prefix +
144
+ chalk[from ? "reset" : "grey"](
145
+ this.formatSpaces(message.decrypted, prefix)
146
+ )
147
+ );
142
148
  }
143
149
 
144
150
  onBeforeClose() {
145
- delete this.environment.room
146
- this.stop = true
151
+ delete this.environment.room;
152
+ this.stop = true;
147
153
  }
148
154
 
149
155
  async postRun(options = {}) {
150
- let cmd = options.cmd.split(' ')
151
- let command = cmd[0]
156
+ let cmd = options.cmd.split(" ");
157
+ let command = cmd[0];
152
158
  if (/^\//.test(command) || !this.basicCommands.includes(command)) {
153
- options.cmd = `send -m "${options.cmd.replace(/^\//, '').replace(/"/g, '\\"')}"`
159
+ options.cmd = `send -m "${options.cmd
160
+ .replace(/^\//, "")
161
+ .replace(/"/g, '\\"')}"`;
154
162
  }
155
- await this.exec([options.cmd])
163
+ await this.exec([options.cmd]);
156
164
  }
157
-
158
165
  }
159
166
 
160
- module.exports = ChatPrompt
161
-
162
-
167
+ module.exports = ChatPrompt;
@@ -1,30 +1,24 @@
1
- const cliConfig = require('../cliConfig')
2
- const Commands = require('../commands')
1
+ const cliConfig = require("../cliConfig");
2
+ const Commands = require("../commands");
3
3
 
4
4
  class ChatPromptMock {
5
-
6
5
  async init(options) {
7
- this.secrez = options.secrez
8
- this.commands = (new Commands(this, cliConfig, 'chat')).getCommands()
9
- this.environment = options.environment
6
+ this.secrez = options.secrez;
7
+ this.commands = new Commands(this, cliConfig, "chat").getCommands();
8
+ this.environment = options.environment;
10
9
  }
11
10
 
12
- async run(options) {
13
- }
11
+ async run(options) {}
14
12
 
15
13
  onBeforeClose() {
16
- delete this.environment.room
14
+ delete this.environment.room;
17
15
  }
18
16
 
19
- async start() {
20
- }
17
+ async start() {}
21
18
 
22
- async exec(cmds, noRun) {
23
- }
19
+ async exec(cmds, noRun) {}
24
20
 
25
- async loading() {
26
- }
21
+ async loading() {}
27
22
  }
28
23
 
29
- module.exports = ChatPromptMock
30
-
24
+ module.exports = ChatPromptMock;