aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/serverLib/CommandManager.js
blob: 0c4f0aae0cdd1d6fafb20c9dcebbedd1e055234c (plain)
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
/**
  * Commands / protocol manager- loads, validates and handles command execution
  *
  * Version: v2.0.0
  * Developer: Marzavec ( https://github.com/marzavec )
  * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
  *
  */

const path = require('path');
const didYouMean = require('didyoumean2').default;

// default command modules path
const CmdDir = 'src/commands';

class CommandManager {
  /**
    * Create a `CommandManager` instance for handling commands/protocol
    *
    * @param {Object} core Reference to the global core object
    */
  constructor (core) {
    this.core = core;
    this.commands = [];
    this.categories = [];
    if (!this.core.config.hasOwnProperty('logErrDetailed')) {
      this.core.config.logErrDetailed = false;
    }
  }

  /**
    * (Re)initializes name spaces for commands and starts load routine
    *
    * @return {String} Module errors or empty if none
    */
  loadCommands () {
    this.commands = [];
    this.categories = [];

    const commandImports = this.core.dynamicImports.getImport(CmdDir);
    let cmdErrors = '';
    Object.keys(commandImports).forEach(file => {
      let command = commandImports[file];
      let name = path.basename(file);
      cmdErrors += this.validateAndLoad(command, file, name);
    });

    return cmdErrors;
  }

  /**
    * Checks the module after having been `require()`ed in and reports errors
    *
    * @param {Object} command reference to the newly loaded object
    * @param {String} file file path to the module
    * @param {String} name command (`cmd`) name
    *
    * @return {String} Module errors or empty if none
    */
  validateAndLoad (command, file, name) {
    let error = this.validateCommand(command);

    if (error) {
      let errText = `Failed to load '${name}': ${error}`;
      console.log(errText);
      return errText;
    }

    if (!command.category) {
      let base = path.join(this.core.dynamicImports.base, 'commands');

      let category = 'Uncategorized';
      if (file.indexOf(path.sep) > -1) {
        category = path.dirname(path.relative(base, file))
          .replace(new RegExp(path.sep.replace('\\', '\\\\'), 'g'), '/');
      }

      command.info.category = category;

      if (this.categories.indexOf(category) === -1) {
        this.categories.push(category);
      }
    }

    if (typeof command.init === 'function') {
      try {
        command.init(this.core);
      } catch (err) {
        let errText = `Failed to initialize '${name}': ${err}`;
        console.log(errText);
        return errText;
      }
    }

    this.commands.push(command);

    return '';
  }

  /**
    * Checks the module after having been `require()`ed in and reports errors
    *
    * @param {Object} object reference to the newly loaded object
    *
    * @return {String} Module errors or null if none
    */
  validateCommand (object) {
    if (typeof object !== 'object')
      return 'command setup is invalid';

    if (typeof object.run !== 'function')
      return 'run function is missing';

    if (typeof object.info !== 'object')
      return 'info object is missing';

    if (typeof object.info.name !== 'string')
      return 'info object is missing a valid name field';

    return null;
  }

  /**
    * Pulls all command names from a passed `category`
    *
    * @param {String} category [Optional] filter return results by this category
    *
    * @return {Array} Array of command modules matching the category
    */
  all (category) {
    return !category ? this.commands : this.commands.filter(
        c => c.info.category.toLowerCase() === category.toLowerCase()
      );
  }

  /**
    * Pulls all category names
    *
    * @return {Array} Array of sub directories under CmdDir
    */
  get categoriesList () {
    return this.categories;
  }

  /**
    * Pulls command by name or alia(s)
    *
    * @param {String} name name or alias of command
    *
    * @return {Object} Target command module object
    */
  get (name) {
    return this.findBy('name', name)
      || this.commands.find(
        command => command.info.aliases instanceof Array &&
        command.info.aliases.indexOf(name) > -1
      );
  }

  /**
    * Pulls command by arbitrary search of the `module.info` attribute
    *
    * @param {String} key name or alias of command
    * @param {String} value name or alias of command
    *
    * @return {Object} Target command module object
    */
  findBy (key, value) {
    return this.commands.find(c => c.info[key] === value);
  }

  /**
    * Runs `initHooks` function on any modules that utilize the event
    *
    * @param {Object} server main server object
    */
  initCommandHooks (server) {
    this.commands.filter(c => typeof c.initHooks !== 'undefined').forEach(
        c => c.initHooks(server)
      );
  }

  /**
    * Finds and executes the requested command, or fails with semi-intelligent error
    *
    * @param {Object} server main server reference
    * @param {Object} socket calling socket reference
    * @param {Object} data command structure passed by socket (client)
    *
    * @return {*} Arbitrary module return data
    */
  handleCommand (server, socket, data) {
    // Try to find command first
    let command = this.get(data.cmd);

    if (command) {
      return this.execute(command, server, socket, data);
    } else {
      // Then fail with helpful (sorta) message
      return this.handleFail(server, socket, data);
    }
  }

  /**
    * Requested command failure handler, attempts to find command and reports back
    *
    * @param {Object} server main server reference
    * @param {Object} socket calling socket reference
    * @param {Object} data command structure passed by socket (client)
    *
    * @return {*} Arbitrary module return data
    */
  handleFail (server, socket, data) {
    const maybe = didYouMean(data.cmd, this.all().map(c => c.info.name), {
      threshold: 5,
      thresholdType: 'edit-distance'
    });

    if (maybe) {
      // Found a suggestion, pass it on to their dyslexic self
      return this.handleCommand(server, socket, {
        cmd: 'socketreply',
        cmdKey: server.cmdKey,
        text: `Command not found, did you mean: \`${maybe}\`?`
      });
    }

    // Request so mangled that I don't even. . .
    return this.handleCommand(server, socket, {
      cmd: 'socketreply',
      cmdKey: server.cmdKey,
      text: 'Unknown command'
    });
  }

  /**
    * Attempt to execute the requested command, fail if err or bad params
    *
    * @param {Object} command target command module
    * @param {Object} server main server reference
    * @param {Object} socket calling socket reference
    * @param {Object} data command structure passed by socket (client)
    *
    * @return {*} Arbitrary module return data
    */
  async execute (command, server, socket, data) {
    if (typeof command.requiredData !== 'undefined') {
      let missing = [];
      for (let i = 0, len = command.requiredData.length; i < len; i++) {
        if (typeof data[command.requiredData[i]] === 'undefined')
          missing.push(command.requiredData[i]);
      }

      if (missing.length > 0) {
        console.log(`Failed to execute '${
            command.info.name
          }': missing required ${missing.join(', ')}\n\n`);

        this.handleCommand(server, socket, {
          cmd: 'socketreply',
          cmdKey: server.cmdKey,
          text: `Failed to execute '${
              command.info.name
            }': missing required ${missing.join(', ')}\n\n`
        });

        return null;
      }
    }

    try {
      return await command.run(this.core, server, socket, data);
    } catch (err) {
      let errText = `Failed to execute '${command.info.name}': `;
      
      // If we have more detail enabled, then we get the trace
      // if it isn't, or the property doesn't exist, then we'll get only the message
      if (this.core.config.logErrDetailed === true) {
        console.log(errText + err.stack);
      } else {
        console.log(errText + err.toString())
      }

      this.handleCommand(server, socket, {
        cmd: 'socketreply',
        cmdKey: server.cmdKey,
        text: errText + err.toString()
      });

      return null;
    }
  }
}

module.exports = CommandManager;