-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.lua
161 lines (148 loc) · 5.04 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
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
--[[
_ _ __ _ _ _____ ______ _____ _____
| | | | | \ | | | | | ___| | __ | | __ \ | ___|
| | | | | \ | | | | | | | | | | | | \ \ | |___
| | | | | |\ \| | | | | | | | | | | | | | | ___|
| |_| | | | \ | | | | |___ | |__| | | |_ / / | |___
|_____| |_| \__| |_| |_____| |______| |_____/ |_____|
____ ____ ____ ______ _____ _____
| _ \ / __ \ | _ \ / ____| | ___| | _ \
| |_| | | |__| | | |_| | | |____ | |___ | |_| |
| ___/ | __ | | _/ \____ \ | ___| | _/
| | | | | | | |\ \ ____| | | |___ | |\ \
|_| |_| |_| |_| \_\ |______/ |_____| |_| \_\
written by (C) 2018 Yaya MNH48 and contributors
released under The MIT License
--]]
-- Load support for intllib.
-- local S, NS = dofile("/intllib.lua")
local texttemp = "" -- declare as empty string
local serverprotocol -- declare the name without type
-- on connect to server, gets server protocol version
minetest.register_on_mods_loaded(function()
serverprotocol = minetest.get_server_info().protocol_version
end)
-- command .uc for cli version
minetest.register_chatcommand("uc", {
params = "<escapecode>",
description = "Directly convert and send out converted unicode escape.",
func = function(param)
texttemp = string.gsub(param, "\\u", "\\0x")
sendForProcess(texttemp,"cli")
end
})
-- command .ug for gui version
minetest.register_chatcommand("ug", {
--description = S("Open the GUI to paste unicode escape, then press \"Say\" to send in chat."),
description = "Open the GUI to paste unicode escape, then press \"Say\" to send in chat.",
func = function(name, escuni)
minetest.show_formspec("unicodeparser:upgui",
"size[6,3]"..
"field[1,1;5,2;text;Input the unicode escape;]"..
"button_exit[2,2;2,1;say;Say]")
end
})
-- define what to do when gui accepted input
minetest.register_on_formspec_input(function(formname, fields)
if formname == "unicodeparser:upgui" then
texttemp = string.gsub(fields.text, "\\u", "\\0x")
sendForProcess(texttemp, "gui")
elseif formname == "unicodeparser:uperr" then
return false
else
return true
end
end)
-- parse the input escaped strings
function sendForProcess(texts,interface)
local allinput=half(texts,"\\")
if checkMessagePermission() == true then
displayError(interface,"Unfortunately, this server does not allow you to send chat message using CSM, so this mod will not work.")
else
local toProcess = {}
if allinput == nil then
displayError(interface,"Please input some text!")
return false
else
for i,line in ipairs(allinput) do
local lineTemp = utf8(tonumber(line))
table.insert(toProcess,lineTemp)
end
local finalOut = table.concat(toProcess)
if serverprotocol < 32 then
minetest.display_chat_message("You wrote: "..finalOut)
end
minetest.send_chat_message(finalOut)
return true
end
end
end
-- split the input escaped strings
function half(inStr, inToken)
if inStr == nil then
return nil
else
local TableWord = {}
local fToken = "(.-)" .. inToken
local l_end = 1
local w, t, halfPos = inStr:find(fToken, 1)
while w do
if w ~= 1 or halfPos ~= "" then
table.insert(TableWord,halfPos)
end
l_end = t+1
w, t, halfPos = inStr:find(fToken, l_end)
end
if l_end <= #inStr then
halfPos = inStr:sub(l_end)
table.insert(TableWord, halfPos)
end
return TableWord
end
end
-- convert the strings back to original unicode
do
local string_char = string.char
function utf8(codep)
if codep < 128 then
return string_char(codep)
end
local s = ""
local max_pf = 32
while true do
local suffix = codep % 64
s = string_char(128 + suffix)..s
codep = (codep - suffix) / 64
if codep < max_pf then
return string_char((256 - (2 * max_pf)) + codep)..s
end
max_pf = max_pf / 2
end
end
end
-- check if server allow sending message from CSM
function checkMessagePermission()
local restr = minetest.get_csm_restrictions()["chat_messages"]
return restr
end
-- display error according to message interface
function displayError(interface,text)
if interface == "cli" then
print("[unicodeparser] ERROR: "..text)
elseif interface == "gui" then
minetest.after(1, showGui, text)
else
return false
end
end
-- show the actual GUI
function showGui(text)
minetest.show_formspec("unicodeparser:guierr",
"size[6,6]"..
"label[2.5,1;".."ERROR".."]"..
"textarea[1,2;5,4;;"..text..";]"..
"button_exit[2,5;2,1;ok;OK]")
end
-- log the CSM
local msg = "[unicodeparser] CSM loaded."
minetest.log("info", msg)