This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugins.js
111 lines (106 loc) · 3.5 KB
/
plugins.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
/**
* Extend an IRC client with plugin functionality.
*
* Should handle:
*
* * Get a list of loaded plugins
* * Load a plugin
* * Unload a plugin
* * Handle help for a plugin
* * Show detailed info for a plugin
*
* @module plugins
*/
'use strict';
const util = require('util');
function addPlugin(plugin) {
try {
let client = this.client;
if (!pluginHasInfo(plugin)) {
let errorMsg = 'Plugin does not provide valid info. ' +
'Requires .info attribute with id, ' +
'version, and description.';
throw new TypeError(errorMsg);
}
plugin.activateOn(client);
this.plugins.push(plugin.info);
} catch (e) {
util.log("Failed to load plugin " + plugin + ": " + e);
}
}
function pluginHasInfo(plugin) {
return typeof(plugin.info) === 'object' &&
typeof(plugin.info.id) === 'string' &&
typeof(plugin.info.version) === 'string' &&
typeof(plugin.info.description) === 'string';
}
/**
* Adds a listener to the client parameter that handles !help messages.
*/
function enableHelp(client) {
client.addListener('message#', function(from, to, text) {
if (text.startsWith('!help')) {
let query = text.substring(6).split(' ');
if (query.length === 1 && query[0] === '') {
let loadedPlugins = this.plugins.plugins
.map(function(p) { return p.id; })
.join(', ');
let reply = 'To get help about a plugin, type !help PLUGINNAME. ';
reply += 'Loaded plugins: ' + loadedPlugins;
client.say(to, reply);
} else if (query.length === 1) {
// Find the plugin
let plugin = this.plugins.plugins.find(function(ele, _idx, _arr) {
return ele.id === query[0];
});
if (plugin === undefined) {
client.say(to, 'No such plugin: ' + query[0]);
return;
}
let reply = 'Plugin: ' + plugin.id +
' (v' + plugin.version + '); ' +
plugin.description +
' Commands: ' + plugin
.commands
.map(function(c) { return c.trigger; })
.join(', ') +
'. More info with !help ' + plugin.id + ' NUMBER.';
client.say(to, reply);
} else if (query.length === 2) {
// Find the plugin
let plugin = this.plugins.plugins.find(function(ele, _idx, _arr) {
return ele.id === query[0];
});
if (plugin === undefined) {
client.say(to, 'No such plugin: ' + query[0]);
return;
}
let idx = parseInt(query[1]);
if (idx < 1 || idx > plugin.commands.length) {
client.say(to, 'Incorrect command number for plugin ' +
plugin.id + '.');
return;
}
let command = plugin.commands[idx - 1];
client.say(to, 'Help for ' + command.trigger + ': ' +
command.description);
} else {
client.say(to, 'Invalid !help command');
}
}
});
}
/**
* Adds plugin features to an IRC client object. After application, the client
* supports
*
* .plugins.add: Adds a given plugin
* .plugins.plugins: List of info for loaded plugins
*/
exports.enablePlugins = function(client) {
// Need a reference to the enclosing one
client.plugins = {client: client};
client.plugins.add = addPlugin;
client.plugins.plugins = [];
enableHelp(client);
};