-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphantom.lua
More file actions
247 lines (195 loc) · 6.6 KB
/
phantom.lua
File metadata and controls
247 lines (195 loc) · 6.6 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
require "utils"
require "menu"
require "windows"
-- Zero-width characters for steganographic encoding
local ZWS = "\xE2\x80\x8B" -- U+200B Zero Width Space (bit 0)
local ZWNJ = "\xE2\x80\x8C" -- U+200C Zero Width Non-Joiner (bit 1)
local ZWJ = "\xE2\x80\x8D" -- U+200D Zero Width Joiner (marker)
local BITS = 16 -- bits per character (covers full BMP)
local function encode(message)
local parts = { ZWJ }
for i = 1, #message do
local cp = string.byte(message, i)
for bit = BITS - 1, 0, -1 do
local value = math.floor(cp / (2 ^ bit)) % 2
parts[#parts + 1] = value == 1 and ZWNJ or ZWS
end
end
parts[#parts + 1] = ZWJ
return table.concat(parts)
end
local function decode(text)
local messages = {}
local pattern = ZWJ .. "(.-)" .. ZWJ
for hidden in text:gmatch(pattern) do
local chars = {}
local bits = {}
for i = 1, #hidden, 3 do
local char = hidden:sub(i, i + 2)
if char == ZWNJ then
bits[#bits + 1] = 1
elseif char == ZWS then
bits[#bits + 1] = 0
end
if #bits == BITS then
local cp = 0
for j = 1, BITS do
cp = cp * 2 + bits[j]
end
chars[#chars + 1] = string.char(cp)
bits = {}
end
end
if #chars > 0 then
messages[#messages + 1] = table.concat(chars)
end
end
return messages
end
local function cleanText(text)
return text:gsub(ZWS, ""):gsub(ZWNJ, ""):gsub(ZWJ, "")
end
-- Commands
local function hide(_, query)
local secret = query:getArgs()
if secret == "" then
return query:answer "Usage: hide <secret message>"
end
query:answer(encode(secret))
end
local function reveal(_, query)
local text = query:replaceExpression("")
local messages = decode(text)
if #messages == 0 then
return menu.create(query, { "No hidden messages found" })
end
local result = { "Hidden messages found: " .. #messages .. "\n\n" }
for i, msg in ipairs(messages) do
result[#result + 1] = "#" .. i .. ": " .. msg .. "\n"
end
result[#result + 1] = "\n"
result[#result + 1] = {
caption = "[OK]",
action = function(_, q)
q:answer(text)
end
}
menu.create(query, result)
end
local function clean(input, query)
inline:setText(input, cleanText(query:replaceExpression("")))
end
local function inspect(_, query)
local text = query:replaceExpression("")
local cleaned = cleanText(text)
local hiddenCount = 0
for _ in text:gmatch(ZWS) do hiddenCount = hiddenCount + 1 end
for _ in text:gmatch(ZWNJ) do hiddenCount = hiddenCount + 1 end
for _ in text:gmatch(ZWJ) do hiddenCount = hiddenCount + 1 end
local messages = decode(text)
local result = {
"Phantom Inspect\n\n",
"Visible length: " .. #cleaned .. " bytes\n",
"Total length: " .. #text .. " bytes\n",
"Hidden chars: " .. hiddenCount .. "\n",
"Hidden messages: " .. #messages .. "\n",
}
for i, msg in ipairs(messages) do
result[#result + 1] = "\n#" .. i .. ": " .. msg
end
result[#result + 1] = "\n"
result[#result + 1] = {
caption = "[OK]",
action = function(_, q)
q:answer(text)
end
}
menu.create(query, result)
end
-- Floating reveal window
local function freveal(_, query)
windows.create({ noLimits = true }, function(ui)
local text = query:replaceExpression("")
local messages = decode(text)
local content
if #messages == 0 then
content = ui.text "No hidden messages found"
else
local parts = {}
for i, msg in ipairs(messages) do
parts[#parts + 1] = "#" .. i .. ": " .. msg
end
content = ui.text(table.concat(parts, "\n"))
end
content:setMaxLines(15)
local paste = ui.smallButton("Paste", function()
if #messages > 0 then
if not windows.insertText(messages[1]) then
return inline:toast "Please focus on the desired input"
end
end
end)
paste:setEnabled(#messages > 0 and windows.isInsertAvailable())
ui.onFocusChanged = function(isFocused)
paste:setEnabled(not isFocused and #messages > 0 and windows.isInsertAvailable())
end
return {
content,
ui.spacer(8),
{
ui.smallButton("Close", function()
ui:close()
end),
ui.spacer(8),
paste
}
}
end)
query:answer()
end
-- Floating hide composer
local function fhide(_, query)
windows.create({ noLimits = true }, function(ui)
local secretInput = ui.textInput "Secret message"
secretInput:getEditText():setMaxLines(5)
secretInput:setText(query:getArgs())
local paste = ui.smallButton("Paste", function()
local secret = secretInput:getText()
if secret == "" then
return inline:toast "Enter a hidden message"
end
if not windows.insertText(encode(secret)) then
return inline:toast "Please focus on the desired input"
end
end)
paste:setEnabled(windows.isInsertAvailable())
ui.onFocusChanged = function(isFocused)
paste:setEnabled(not isFocused and windows.isInsertAvailable())
end
return {
secretInput,
ui.spacer(8),
{
ui.smallButton("Close", function()
ui:close()
end),
ui.spacer(8),
paste
}
}
end)
query:answer()
end
return function(module)
module:setCategory "Phantom"
module:registerCommand("hide", utils.hasArgs(hide), "Encodes a secret into invisible zero-width characters")
module:registerCommand("reveal", reveal, "Decodes hidden messages from the text")
module:registerCommand("clean", clean, "Removes all invisible characters from text")
module:registerCommand("inspect", inspect, "Shows stats about hidden content in text")
if windows.isSupported() then
module:registerCommand("freveal", freveal, "Floating window to reveal hidden messages")
module:registerCommand("fhide", fhide, "Floating composer for hiding messages")
windows.supportInsert()
end
module:saveLazyLoad()
end