forked from cuberite/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_plugins.lua
More file actions
322 lines (250 loc) · 9.89 KB
/
web_plugins.lua
File metadata and controls
322 lines (250 loc) · 9.89 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
-- web_plugins.lua
-- Implements the Plugins web tab used to manage plugins on the server
--[[
General info: The web handler loads the settings.ini file in its start, and reads the list of enabled
plugins out of it. Then it processes any changes requested by the user through the buttons; it carries out
those changes on the list of enabled plugins itself. Then it saves that list back to the settings.ini. The
changes aren't applied until the user expliticly clicks on "reload", since some changes require more than
single reloads of the page (such as enabling a plugin and moving it into place using the up / down buttons).
--]]
-- Stores whether the plugin list has changed and thus the server needs to reload plugins
-- Has to be defined outside so that it keeps its value across multiple calls to the handler.
local g_NeedsReload = false
--- Returns an array of plugin names that are enabled, in their load order
local function LoadEnabledPlugins(SettingsIni)
local res = {};
local IniKeyPlugins = SettingsIni:FindKey("Plugins")
if (IniKeyPlugins == cIniFile.noID) then
-- No [Plugins] key in the INI file
return {}
end
-- Scan each value, remember each that is named "plugin"
for idx = 0, SettingsIni:GetNumValues(IniKeyPlugins) - 1 do
if (string.lower(SettingsIni:GetValueName(IniKeyPlugins, idx)) == "plugin") then
table.insert(res, SettingsIni:GetValue(IniKeyPlugins, idx))
end
end
return res
end
--- Saves the list of enabled plugins into the ini file
-- Keeps all the other values in the ini file intact
local function SaveEnabledPlugins(SettingsIni, EnabledPlugins)
-- First remove all values named "plugin":
local IniKeyPlugins = SettingsIni:FindKey("Plugins")
if (IniKeyPlugins ~= cIniFile.noID) then
for idx = SettingsIni:GetNumValues(IniKeyPlugins) - 1, 0, -1 do
if (string.lower(SettingsIni:GetValueName(IniKeyPlugins, idx)) == "plugin") then
SettingsIni:DeleteValueByID(IniKeyPlugins, idx)
end
end
end
-- Now add back the entire list of enabled plugins, in our order:
for idx, name in ipairs(EnabledPlugins) do
SettingsIni:AddValue("Plugins", "Plugin", name)
end
-- Save to file:
SettingsIni:WriteFile("settings.ini")
-- Mark the settings as changed:
g_NeedsReload = true
end
--- Builds an HTML table containing the list of plugins
-- First the loaded plugins are listed in their load order
-- Then any plugins that are enabled but failed to load are listed alphabetically
-- Finally an alpha-sorted list of the disabled plugins
local function ListCurrentPlugins(EnabledPlugins)
-- Retrieve a map of all known plugins:
local PM = cPluginManager:Get()
PM:FindPlugins()
local PluginMap = PM:GetAllPlugins()
-- Separate all enabled but not loaded plugins into ErrorPlugins array
-- Put all successfully loaded plugins to LoadedPlugins array
-- Also remove the enabled plugins from PluginList
local ErrorPlugins = {}
local LoadedPlugins = {}
for idx, name in ipairs(EnabledPlugins) do
if not(PluginMap[name]) then
table.insert(ErrorPlugins, name)
else
table.insert(LoadedPlugins, name)
end
PluginMap[name] = nil
end
-- Put all known but not enabled plugins into DisabledPlugins array
local DisabledPlugins = {}
for name, plugin in pairs(PluginMap) do
table.insert(DisabledPlugins, name)
end
-- Sort the plugin arrays:
table.sort(ErrorPlugins)
table.sort(DisabledPlugins)
-- Do NOT sort LoadedPlugins - we want them listed in their load order instead!
-- Output the LoadedPlugins table:
local res = {}
local ins = table.insert
if (#LoadedPlugins > 0) then
ins(res, [[
<h4>Loaded plugins</h4>
<p>These plugins have been successfully initialized and are currently running.</p>
<table>
]]
);
local Num = #LoadedPlugins
for idx, name in pairs(LoadedPlugins) do
ins(res, [[<tr><td width="100%">]])
ins(res, name)
ins(res, [[</td><td>]])
if (idx == 1) then
ins(res, [[<button type="button" disabled>Move Up</button> </td>]])
else
ins(res, '<form method="POST"><input type="hidden" name="PluginName" value="')
ins(res, name)
ins(res, '"><input type="submit" name="MoveUp" value="Move Up"></form></td>')
end
ins(res, [[</td><td>]])
if (idx == Num) then
ins(res, '<button type="button" disabled>Move Down</button></td>')
else
ins(res, '<form method="POST"><input type="hidden" name="PluginName" value="')
ins(res, name)
ins(res, '"><input type="submit" name="MoveDown" value="Move Down"></form></td>')
end
ins(res, '<td><form method="POST"><input type="hidden" name="PluginName" value="')
ins(res, name)
ins(res, '"><input type="submit" name="DisablePlugin" value="Disable"></form></td></tr>')
end
ins(res, "</table><br />")
end
-- Output ErrorPlugins table:
if (#ErrorPlugins > 0) then
ins(res, [[
<hr /><h4>Errors</h4>
<p>These plugins are configured to run, but encountered a problem during their initialization.
MCServer disabled them temporarily and will try reloading them next time.</p>
<table>]]
)
for idx, name in ipairs(ErrorPlugins) do
ins(res, "<tr><td width=\"100%\">")
ins(res, name)
ins(res, "</td><td><form method='POST'><input type='hidden' name='PluginName' value='")
ins(res, name)
ins(res, "'><input type='submit' name='DisablePlugin' value='Disable'></form></td></tr>")
end
ins(res, "</table><br />")
end
-- Output DisabledPlugins table:
if (#DisabledPlugins > 0) then
ins(res, [[<hr /><h4>Disabled plugins</h4>
<p>These plugins are installed, but are disabled in the configuration.</p>
<table>]]
)
for idx, name in ipairs(DisabledPlugins) do
ins(res, "<tr><td width=\"100%\">")
ins(res, name)
ins(res, '</td><td><form method="POST"><input type="hidden" name="PluginName" value="')
ins(res, name)
ins(res, '"><input type="submit" name="EnablePlugin" value="Enable"></form></td></tr>')
end
ins(res, "</table><br />")
end
return table.concat(res, "")
end
--- Disables the specified plugin
-- Saves the new set of enabled plugins into SettingsIni
-- Returns true if the plugin was disabled
local function DisablePlugin(SettingsIni, PluginName, EnabledPlugins)
for idx, name in ipairs(EnabledPlugins) do
if (name == PluginName) then
table.remove(EnabledPlugins, idx)
SaveEnabledPlugins(SettingsIni, EnabledPlugins)
return true
end
end
return false
end
--- Enables the specified plugin
-- Saves the new set of enabled plugins into SettingsIni
-- Returns true if the plugin was enabled (false if it was already enabled before)
local function EnablePlugin(SettingsIni, PluginName, EnabledPlugins)
for idx, name in ipairs(EnabledPlugins) do
if (name == PluginName) then
-- Plugin already enabled, ignore this call
return false
end
end
-- Add the plugin to the end of the list, save:
table.insert(EnabledPlugins, PluginName)
SaveEnabledPlugins(SettingsIni, EnabledPlugins)
end
--- Moves the specified plugin up or down by the specified delta
-- Saves the new order into SettingsIni
-- Returns true if the plugin was moved, false if not (bad delta / not found)
local function MovePlugin(SettingsIni, PluginName, IndexDelta, EnabledPlugins)
for idx, name in ipairs(EnabledPlugins) do
if (name == PluginName) then
local DstIdx = idx + IndexDelta
if ((DstIdx < 1) or (DstIdx > #EnabledPlugins)) then
LOGWARNING("Core WebAdmin: Requesting moving the plugin " .. PluginName .. " to invalid index " .. DstIdx .. " (max idx " .. #EnabledPlugins .. "); ignoring.")
return false
end
EnabledPlugins[idx], EnabledPlugins[DstIdx] = EnabledPlugins[DstIdx], EnabledPlugins[idx] -- swap the two - we're expecting ony +1 / -1 moves
SaveEnabledPlugins(SettingsIni, EnabledPlugins)
return true
end
end
-- Plugin not found:
return false
end
--- Processes the actions specified by the request parameters
-- Modifies EnabledPlugins directly to reflect the action
-- Returns the notification text to be displayed at the top of the page
local function ProcessRequestActions(SettingsIni, Request, EnabledPlugins)
local PluginName = Request.PostParams["PluginName"];
if (PluginName == nil) then
-- No PluginName was provided, so there's no action to perform
return
end
if (Request.PostParams["DisablePlugin"] ~= nil) then
if (DisablePlugin(SettingsIni, PluginName, EnabledPlugins)) then
return '<td><p style="color: green;"><b>You disabled plugin: "' .. PluginName .. '"</b></p>'
end
elseif (Request.PostParams["EnablePlugin"] ~= nil) then
if (EnablePlugin(SettingsIni, PluginName, EnabledPlugins)) then
return '<td><p style="color: green;"><b>You enabled plugin: "' .. PluginName .. '"</b></p>'
end
elseif (Request.PostParams["MoveUp"] ~= nil) then
MovePlugin(SettingsIni, PluginName, -1, EnabledPlugins)
elseif (Request.PostParams["MoveDown"] ~= nil) then
MovePlugin(SettingsIni, PluginName, 1, EnabledPlugins)
end
end
function HandleRequest_ManagePlugins(Request)
local Content = ""
if (Request.PostParams["reload"] ~= nil) then
Content = Content .. "<head><meta http-equiv=\"refresh\" content=\"5;\"></head>"
Content = Content .. "<p>Reloading plugins... This can take a while depending on the plugins you're using.</p>"
cRoot:Get():GetPluginManager():ReloadPlugins()
return Content
end
local SettingsIni = cIniFile()
SettingsIni:ReadFile("settings.ini")
local EnabledPlugins = LoadEnabledPlugins(SettingsIni)
local NotificationText = ProcessRequestActions(SettingsIni, Request, EnabledPlugins)
Content = Content .. (NotificationText or "")
if (g_NeedsReload) then
Content = Content .. [[
<form method='POST'>
<p class="warn"><b>
You need to reload the plugins in order for the changes to take effect.
<input type='submit' name='reload' value='Reload now!'>
</b></p></form>
]]
end
Content = Content .. ListCurrentPlugins(EnabledPlugins)
Content = Content .. [[<hr />
<h4>Reload</h4>
<form method='POST'>
<p>Click the reload button to reload all plugins.
<input type='submit' name='reload' value='Reload!'></p>
</form>]]
return Content
end