-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.js
More file actions
195 lines (164 loc) · 7.09 KB
/
plugin.js
File metadata and controls
195 lines (164 loc) · 7.09 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
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
const format = require("util").format;
const inspect = require("util").inspect;
const Promise = require("bluebird");
const anyRegex = /.*/;
const names = ["nickname", "username", "hostname"];
const adminKeys = names.concat("identifiedas");
// Hostmask :: {nickname: String, username: String, hostname: String}
// Privmsg :: tennu::IRCMessage::Privmsg
// Admin :: {nickname: RegExp, username: RegExp, hostname: RegExp, identifiedas: String?}
const allowAllSymbol = Symbol("Admin Command - Allow all");
function notHasIdentifiedasProperty (admin) {
return !admin["identifiedas"];
}
function cloneOnlyAdminKeys (object) {
const clone = {};
adminKeys.forEach(function (key) {
clone[key] = object[key];
});
return clone;
}
module.exports = AdminModule = {
configDefaults: {
"admin-failed-attempt-response": "Permission denied.",
"admin-commands": []
},
init: function (client, imports) {
const isIdentifiedAs = imports.user.isIdentifiedAs;
var admins;
const deniedResponse = client.config("admin-failed-attempt-response");
const forcedAdminCommands = client.config("admin-commands");
// tennu.Client! -> {nickname: String?, username: String?, hostname: String?, identifiedas: String?} -> Admin
function regexify (admin) {
client.note("PluginAdmin", format("Adding admin: %j", admin));
names.forEach(function (name) {
if (admin[name]) {
admin[name] = new RegExp(admin[name], "i");
} else {
admin[name] = anyRegex;
}
});
return admin;
}
// tennu.Client! -> [Admin] throws Error
function initalizeAdmins (adminConfigObject) {
const admins = adminConfigObject || client.config("admins");
if (!Array.isArray(admins)) {
const errormsg = "\"admins\" property in configuration must be an array";
client.error(errormsg);
throw new Error(errormsg);
}
return admins.map(cloneOnlyAdminKeys).map(regexify);
}
// tennu.Client! -> Hostmask -> Admin -> boolean
function checkHostmask (hostmask, admin) {
return names.every(function (name) {
const result = admin[name].test(hostmask[name]);
client.debug("PluginAdmin", format("%s: %s, %s (%s)",
name, hostmask[name], admin[name], result));
return result;
});
}
// Module Initialization
admins = initalizeAdmins();
// Hostmask -> Promise boolean
const isAdmin = function (hostmask, opts) {
return Promise.try(function () {
if (opts && opts.allowAll === true) {
return true;
}
const customAdmins = opts && opts.customAdmins;
const memoizationKey = opts && opts.memoizeOver;
const hostmask_passed = (customAdmins || admins).filter(function (admin) {
return checkHostmask(hostmask, admin);
});
if (hostmask_passed.some(notHasIdentifiedasProperty)) {
client.debug("PluginAdmin", "Admin object w/o identifiedas property (true)");
return true;
}
client.debug("PluginAdmin", "Admin object w/o identifiedas property (false)");
return (function recur () {
if (hostmask_passed.length === 0) {
client.debug("PluginAdmin", "User passes an identifiedas check (false)");
return false;
}
return Promise.try(function () {
const hostmask = hostmask_passed.pop();
return hostmask.identifiedas;
})
.then(function (accountname) {
return isIdentifiedAs(hostmask.nickname, accountname, {memoizeOver: memoizationKey});
})
.then(function (isIdentifiedAs) {
if (isIdentifiedAs) {
client.debug("PluginAdmin", "User passes an identifiedas check (true)");
return true;
} else {
return recur();
}
});
}());
});
};
// (Privmsg -> Response) -> (Privmsg -> Response)
const requiresAdmin = function (fn) {
return function (privmsg) {
// If the "command" property is set, it's a command, and not a privmsg.
// The prototype of a command is the privmsg though.
const memoizeOver = privmsg.command ? Object.getPrototypeOf(privmsg) : privmsg;
const allowAll = privmsg[allowAllSymbol];
return isAdmin(privmsg.hostmask, {memoizeOver, allowAll})
.then(function (isAdmin) {
if (isAdmin) {
return fn(privmsg);
} else {
return deniedResponse;
}
});
};
};
// (Privmsg, () -> Response) -> Response
const checkAdmin = function (privmsg, adminOnlyCallback) {
// If the "command" property is set, it's a command, and not a privmsg.
// The prototype of a command is the privmsg though.
const memoizeOver = privmsg.command ? Object.getPrototypeOf(privmsg) : privmsg;
const allowAll = privmsg[allowAllSymbol];
return isAdmin(privmsg.hostmask, {memoizeOver, allowAll})
.then(function (isAdmin) {
if (isAdmin) {
return adminOnlyCallback();
} else {
client.note("PluginAdmin", format("'%s' tried to execute admin-only functionality", privmsg.nickname));
return deniedResponse;
}
});
}
return {
commandMiddleware: function(command) {
if (forcedAdminCommands.indexOf(command.command) === -1) {
return command;
}
return isAdmin(command.hostmask, {memoizeOver: Object.getPrototypeOf(command)})
.then(function(isAdmin) {
if (isAdmin) {
return command;
}
client.note("PluginAdmin", format("'%s' tried to run user-defined admin command '%s'", command.nickname, command.command));
return deniedResponse;
});
},
exports: {
isAdmin,
checkAdmin,
requiresAdmin,
allowAll: allowAllSymbol,
initalizeAdmins,
regexify,
checkHostmask,
}
};
},
name: "admin",
role: "admin",
requires: ["config", "user"]
};