summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/Core/web_manageplugins.lua
blob: 543638183594cdc49d6cc5d7368402aec9da8b92 (plain) (blame)
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
local function Button_RemovePlugin( Name, Index )
	return "<form method='POST'><input type='hidden' name='PluginName' value='"..Name.."'><input type='hidden' name='PluginIndex' value='"..Index.."'><input type='submit' name='RemovePlugin' value='Remove'></form>"
end

local function Button_EnablePlugin( Name )
	return [[<form method="POST"><input type="hidden" name="PluginName", value="]].. Name ..[["><input type="submit" name="EnablePlugin" value="Enable"></form>]]
end

local function Button_DisablePlugin( Name )
	return [[<form method="POST"><input type="hidden" name="PluginName", value="]].. Name ..[["><input type="submit" name="DisablePlugin" value="Disable"></form>]]
end

local function FindPluginID( SettingsIni, PluginName )
	local KeyIdx = SettingsIni:FindKey("Plugins")
	local NumValues = SettingsIni:GetNumValues( KeyIdx )
	
	for i = 0, NumValues-1 do
		LOGINFO( SettingsIni:GetValue(KeyIdx, i) )
		if( SettingsIni:GetValue(KeyIdx, i) == PluginName ) then
			return i
		end
	end
	
	return nil
end

local function RemovePluginFromIni( SettingsIni, PluginName )
	local KeyIdx = SettingsIni:FindKey("Plugins")
	local PluginIdx = FindPluginID( SettingsIni, PluginName )
	
	if( PluginIdx == nil ) then
		LOGINFO("Got nil! NOOOO")
		return false
	end
	
	local Name = SettingsIni:GetValue( KeyIdx, PluginIdx )
	if( Name ~= PluginName ) then
		LOGINFO("not the same name T_T '" .. Name .. "' '" .. PluginName .. "'")
	end
	if( (Name == PluginName) and (SettingsIni:DeleteValueByID( KeyIdx, PluginIdx ) == true) ) then
		return SettingsIni:WriteFile()
	end
	
	return false
end

local function AddPluginToIni( SettingsIni, PluginName )
	RemovePluginFromIni( SettingsIni, PluginName ) -- Make sure there are no duplicates
	
	if( SettingsIni:SetValue("Plugins", "Plugin", PluginName, true ) == true ) then
		return SettingsIni:WriteFile()
	end
	
	return false
end

local function HandlePluginListChanges( Request, SettingsIni )
	local Content = ""
			
	if( Request.PostParams["EnablePlugin"] ~= nil
		and Request.PostParams["PluginName"] ~= nil ) then
		
		local PluginName = Request.PostParams["PluginName"]
	
		local PM = cRoot:Get():GetPluginManager()
		if( PM:LoadPlugin( PluginName ) == false ) then
			Content = "Could not enable '".. PluginName .."'!"
		end
	
		if( AddPluginToIni( SettingsIni, PluginName ) == true ) then
			Content = "Enabled plugin '".. PluginName .."'"
		else
			Content = "Enabled plugin '".. PluginName .."' but could not add it to settings.ini"
		end
		
	
	elseif( Request.PostParams["DisablePlugin"] ~= nil
		and Request.PostParams["PluginName"] ~= nil ) then
		
		local PluginName = Request.PostParams["PluginName"]
		
		local PM = cRoot:Get():GetPluginManager()
		PM:DisablePlugin( PluginName )
		
		if( RemovePluginFromIni( SettingsIni, PluginName ) == true ) then
			Content = "Disabled plugin '".. PluginName .."'"
		else
			Content = "Disabled plugin '".. PluginName .."' but could not remove it from settings.ini"
		end
		
		
		
	end
	
	if( #Content > 0 ) then
		return "<p><font color='red'><strong>INFO: " .. Content .. "</strong></font></p>"
	else
		return ""
	end
end

function HandleRequest_ManagePlugins( Request )
	local Content = ""
	
	if( Request.PostParams["reload"] ~= nil ) then
		Content = Content .. "<head><meta http-equiv=\"refresh\" content=\"2;\"></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("settings.ini")
	if( SettingsIni:ReadFile() == true ) then
		Content = Content .. HandlePluginListChanges( Request, SettingsIni )
	else
		Content = Content .. "Cannot find/modify settings.ini"
	end
	
	local PluginManager = cRoot:Get():GetPluginManager()
	PluginManager:FindPlugins() -- Refreshes the plugin list
	local PluginList = PluginManager:GetAllPlugins()
	
	Content = Content .. "<h4>Currently installed plugins</h4>"
	Content = Content .. "<table>"
	ActivePluginsName = {}
	ActivePluginVersion = {}
	InactivePlugins = {}
	for k, Plugin in pairs(PluginList) do
		if( Plugin ) then
			table.insert( ActivePluginsName, k )
			table.insert( ActivePluginVersion, Plugin:GetVersion() )
		else
			table.insert( InactivePlugins, k )
		end
	end
	table.sort( ActivePluginsName )
	table.sort( InactivePlugins )
	for i = 1, #ActivePluginsName do
		Content = Content .. "<tr><td>".. ActivePluginsName[i] .."</td>"
		Content = Content .. "<td>" .. ActivePluginsName[i] .. " V. " .. ActivePluginVersion[i] .. "</td><td>" .. Button_DisablePlugin(ActivePluginsName[i]) .. "</td>"
		Content = Content .. "</tr>"
	end
	for i = 1, #InactivePlugins do
		Content = Content .. "<tr><td>".. InactivePlugins[i] .."</td>"
		Content = Content .. "<td></td><td>" .. Button_EnablePlugin(InactivePlugins[i]) .. "</td>"
		Content = Content .. "</tr>"
	end
	Content = Content .. "</table>"
	
	Content = Content .. "<h4>Reload</h4>"
	Content = Content .. "<form method='POST'>"
	Content = Content .. "<p>Click the reload button to reload all plugins according to <strong>settings.ini</strong>!"
	Content = Content .. "<input type='submit' name='reload' value='Reload!'></p>"
	Content = Content .. "</form>"

	return Content
end