This repository was archived by the owner on Feb 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
88 lines (79 loc) · 2.48 KB
/
init.lua
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
shell_mod = {}
local history = {}
-- INITLIB
local S, NS
if minetest.global_exists('intllib') then
S, NS = intllib.make_gettext_pair(minetest.get_current_modname())
else
S = function(s) return s end
NS = S
end
-- LOCAL FUNCTIONS
local function get_shell(player_name, old, cmd)
local str = 'size[20.25,9]textlist[0,0;20,8;shell_mod:box;'
local len = 0
if old then
for _, line in ipairs(old) do
str = str .. line .. ','
len = len + 1
end
end
if cmd then
local log_cmd = player_name .. ': ' .. cmd
local date = minetest.formspec_escape(os.date('[%Y-%m-%d %X]: '))
table.insert(history, date .. log_cmd)
str = str .. date .. log_cmd .. ','
minetest.log('warning', '[shell_mod] command: ' .. log_cmd)
len = len + 1
local handler = io.popen(cmd)
for line in handler:lines() do
line = minetest.formspec_escape(line)
str = str .. date .. line .. ','
table.insert(history, date .. line)
minetest.log('warning', '[shell_mod] out: ' .. line)
len = len + 1
end
handler:close()
end
str = str .. ';' .. tostring(len) .. ']field[0.30,8.3;17,1;shell_mod:cmd;;]button[17.25,8;3,1;shell_mod:go;' .. S('Go') .. ']'
return str
end
-- API
function shell_mod.open_shell(player_name, old, cmd)
minetest.show_formspec(player_name, 'shell_mod', get_shell(player_name, old, cmd))
end
function shell_mod.get_history()
return history
end
function shell_mod.clear_history()
history = {}
end
-- EVENTS
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name();
if formname == 'shell_mod' and fields['shell_mod:cmd'] and minetest.check_player_privs(name, {shell_cmd=true}) then
shell_mod.open_shell(name, history, fields['shell_mod:cmd'])
end
end)
-- REGISTRATIONS
minetest.register_privilege('shell_cmd', S('Can use /shell'))
minetest.register_privilege('shell_clear', S('Can use /shell-clear'))
-- COMMANDS
minetest.register_chatcommand('shell', {
params = 'none',
description = S('open shell window'),
privs = {shell_cmd = true},
func = function(name)
shell_mod.open_shell(name, history, nil)
return true
end,
})
minetest.register_chatcommand('shell-clear', {
params = 'none',
description = S('clear shell history'),
privs = {shell_clear = true},
func = function(name)
history = {}
return true, S('shell history cleared')
end,
})