forked from kirjavascript/nibblrjr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.js
More file actions
153 lines (135 loc) · 4.08 KB
/
commands.js
File metadata and controls
153 lines (135 loc) · 4.08 KB
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
const { parseCommand } = require('../irc/evaluate/scripts/parse-command');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const reserved = require('../base/reserved');
function commandPath(name = '') {
return path.join(__dirname, '../commands', name);
}
function commandHash(name) {
const hash = crypto.createHmac('sha1', 'nibblr').update(name).digest('hex').slice(0,12);
const clean = name.replace(/[^a-zA-Z0-9]/g, '').slice(0,128);
return `${clean}-${hash}`;
}
function getCommand(name) {
const filename = commandPath(commandHash(name) + '.json');
if (!fs.existsSync(filename)) return;
return JSON.parse(fs.readFileSync(filename, 'utf8'));
}
function setCommand(obj) {
const filename = commandPath(commandHash(obj.name) + '.json');
fs.writeFileSync(filename, JSON.stringify(obj, null, 4), 'utf8');
}
function deleteCommand(name) {
fs.unlinkSync(commandPath(commandHash(name) + '.json'));
}
function getAllCommands() {
const commands = [];
const commandList = fs.readdirSync(commandPath());
for (let i = 0; i < commandList.length; i++) {
const filename = commandPath(commandList[i]);
commands.push(JSON.parse(fs.readFileSync(filename, 'utf8')));
}
return commands;
}
function createCommandDB() {
const get = (name) => {
const obj = getCommand(name);
if (!obj) return;
const { root } = parseCommand({ text: name });
if (name == root) return obj;
const parent = getCommand(root);
if (!parent) return obj;
return {
...parent,
name,
command: obj.command,
};
};
const list = () => {
// some kind of caching/indexing would improve performance here
const cmdList = getAllCommands();
return cmdList.map(cmd => {
delete cmd.command;
const { root } = parseCommand({ text: cmd.name });
if (cmd.name == root) return cmd;
// const parent = getCommand(root);
const parent = cmdList.find(d => d.name == root);
if (!parent) return cmd;
const obj = {
...parent,
name: cmd.name
};
delete obj.command;
return obj;
});
};
const set = (name, value) => {
const safeName = name.replace(/\s+/g, '');
const options = getCommand(name) || {
locked: false,
starred: false,
};
setCommand({
...options,
name,
command: value,
});
};
const setConfig = (name, config) => {
// only operates on the parent (if it exists)
const { root } = parseCommand({ text: name });
const parent = getCommand(root);
const realName = parent ? root : name;
const obj = getCommand(realName);
setCommand(Object.assign(obj, config));
};
// public API
const names = () => {
return getAllCommands().map(cmd => cmd.name);
};
const count = () => {
return fs.readdirSync(commandPath()).length;
};
const setSafe = (name, value) => {
const obj = get(name);
const isReserved = reserved.includes(name);
const parentCmdName = parseCommand({text: name}).list[0];
const parentCmd = get(parentCmdName);
if (
obj && obj.locked
|| isReserved
|| !obj && parentCmd && parentCmd.locked
) {
return false;
}
else {
set(name, value);
return true;
}
};
const deleteSafe = (name) => {
const obj = get(name);
if (obj && obj.locked) {
return false;
} else {
deleteCommand(name);
return true;
}
};
const getCommandFns = (node) => ({
get,
list,
names,
count,
setSafe,
deleteSafe,
});
return {
get, delete: deleteCommand, set, list, setConfig, getCommandFns,
};
}
module.exports = {
createCommandDB,
commandHash,
};