-
Notifications
You must be signed in to change notification settings - Fork 24
/
msgfulltextwdx.lua
414 lines (398 loc) · 10.3 KB
/
msgfulltextwdx.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
-- msgfulltextwdx.lua (cross-platform)
-- 2021.04.18
--[[
For Find files with plugins only!
Returns full text of saved email messages.
Message body only ("Content-Type: text/plain" or "Content-Type: text/html"), for header use msginfowdx.lua).
Supported formats: *.eml, *.msg and MH format (Sylpheed, Claws Mail and other).
See RFC 822, 2822, 5322 and 2045, 2046, 2047
NOTES:
1) Sylpheed and some others don't use file extension, so we will use EXT="*"
and check file extension in the beginning of ContentGetValue().
2) The first line should contain any email message field!
3) Decoding Base64: https://github.com/toastdriven/lua-base64
with small modifications.
]]
local sbody
local filename = ''
-- Optimisation
local tonum = tonumber
local str_char = string.char
local str_find = string.find
local str_gsub = string.gsub
local str_len = string.len
local str_lower = string.lower
local str_match = string.match
local str_sub = string.sub
function ContentGetSupportedField(FieldIndex)
if FieldIndex == 0 then
return "Full text (body)", "", 9
end
return "", "", 0
end
function ContentGetDefaultSortOrder(FieldIndex)
return 1; --or -1
end
function ContentGetDetectString()
return 'EXT="*"'
end
function ContentGetValue(FileName, FieldIndex, UnitIndex, flags)
if FieldIndex ~= 0 then return nil end
if UnitIndex ~= 0 then return nil end
if filename ~= FileName then
local e = str_lower(SysUtils.ExtractFileExt(FileName))
if (e ~= '.eml') and (e ~= '.msg') and (e ~= '') then return nil end
local at = SysUtils.FileGetAttr(FileName)
if (at < 0) or (math.floor(at / 0x00000010) % 2 ~= 0) then return nil end
local h = io.open(FileName, 'r')
if h == nil then return nil end
local c, hd, hc, bc = 0, 0, 1, 1
local ctv, cte, s
local head = {}
local body = {}
for l in h:lines() do
if c == 0 then
if str_match(l, '^([A-Za-z0-9%-]+)[\t ]*:') == nil then
break
else
head[hc] = l
hc = hc + 1
end
else
if hd == 0 then
if str_len(l) == 0 then
hd = 1
else
head[hc] = l
hc = hc + 1
end
else
body[bc] = l
bc = bc + 1
end
end
c = 2
end
h:close()
for i = 1, #head do
if (ctv ~= nil) and (cte ~= nil) then break end
s = str_match(head[i], '^([A-Za-z%-]+)[\t ]*:')
if s ~= nil then
s = str_lower(s)
if s == 'content-type' then
ctv = head[i]
-- The value may be long
if head[i + 1] ~= nil then
for j = i + 1, #head do
if str_find(head[j], '^%s+', 1, false) ~= nil then
ctv = ctv .. head[j]
else
break
end
end
end
else
if s == 'content-transfer-encoding' then cte = head[i] end
end
end
end
if str_find(ctv, 'multipart/', 1, true) ~= nil then
GetMultiPart(body, ctv)
else
if str_find(ctv, 'text/plain', 1, true) == nil then
if str_find(ctv, 'text/html', 1, true) == nil then return nil end
end
if cte == nil then cte = 'Content-Transfer-Encoding: 8bit' end
sbody = GetSingle(body, ctv, cte)
end
filename = FileName
end
if sbody == '' then return nil else return sbody end
end
function GetSingle(bd, v, e)
local sbd = ''
local d = 1
for i = 1, #bd do
if bd[i] ~= '' then
d = i
break
end
end
local enc = str_match(e, ':%s*([A-Za-z0-9%-]+)')
enc = str_lower(enc)
if enc == 'base64' then
for i = d, #bd do sbd = sbd .. bd[i] end
if sbd ~= '' then sbd = DecodeBase64(sbd) end
elseif enc == 'quoted-printable' then
sbd = bd[d]
for i = d + 1, #bd do
if str_sub(bd[i - 1], -1, -1) == '=' then
sbd = str_sub(sbd, 1, -2) .. bd[i]
else
sbd = sbd .. '\n' .. bd[i]
end
end
if sbd ~= '' then sbd = DecodeQuotedPrintable(sbd) end
else
for i = d, #bd do sbd = sbd .. '\n' .. bd[i] end
end
if sbd ~= '' then
local cset = str_match(v, 'charset%s*=%s*"?([A-Za-z0-9%-]+)"?')
if cset ~= nil then
cset = str_lower(cset)
if cset ~= 'utf-8' then sbd = EncodeToUTF8(sbd, cset) end
end
end
return sbd
end
function GetMultiPart(bd, v)
if str_find(v, 'multipart/alternative', 1, false) ~= nil then
GetMultiPartAlt(bd, v)
else
-- multipart: "related", "signed" and other "mixed"
local t = {}
local c, d, g = 1, 1, 0
local hs, he
local bo = str_match(v, 'boundary="?([^"]+)"?')
-- Get first part (other parts are usualy attached file(s) or signature (PGP, etc)
for i = 1, #bd do
if g == 0 then
if bd[i] == '--' .. bo then
hs = i + 1
g = 1
end
else
if (bd[i] == '--' .. bo) or (bd[i] == '--' .. bo .. '--') then
he = i - 1
break
end
t[c] = bd[i]
c = c + 1
end
end
if he == nil then he = #bd end
for i = hs, he do
if bd[i] ~= '' then
hs = i
break
end
end
d = he - hs
if d < 3 then return nil end
local tv, te, s
for i = hs, he do
if bd[i] == '' then
hs = i + 1
break
end
s = str_match(bd[i], '^([A-Za-z%-]+)[\t ]*:')
if s ~= nil then
s = str_lower(s)
if s == 'content-type' then
tv = bd[i]
if bd[i + 1] ~= nil then
for j = i + 1, #bd do
if str_find(bd[j], '^%s+', 1, false) ~= nil then
tv = tv .. bd[j]
else
break
end
end
end
else
if s == 'content-transfer-encoding' then te = bd[i] end
end
end
end
for i = hs, he do
if bd[i] ~= '' then
hs = i
break
end
end
d = he - hs
if d < 1 then return nil end
if str_find(tv, 'multipart/', 1, true) ~= nil then
g = 2
else
if str_find(tv, 'text/plain', 1, true) == nil then
if str_find(tv, 'text/html', 1, true) == nil then return nil end
end
if te == nil then te = 'Content-Transfer-Encoding: 8bit' end
g = 3
end
if g > 1 then
t = {}
c = 1
for i = hs, he do
t[c] = bd[i]
c = c + 1
end
if g == 2 then
GetMultiPart(t, tv)
elseif g == 3 then
sbody = GetSingle(t, tv, te)
end
end
end
end
function GetMultiPartAlt(b, v)
sbody = ''
local t = {}
local c, d, i, out = 1, 1, 1, 0
local bo = str_match(v, 'boundary="?([^"]+)"?')
while true do
if (out == 1) or (c >= #b) then break end
for j = c, #b do
if b[j] == '--' .. bo then
t = {}
for k = j + 1, #b do
if b[k] == '--' .. bo .. '--' then
out = 1
break
elseif b[k] == '--' .. bo then
c = k
break
end
t[i] = b[k]
i = i + 1
end
break
end
end
if #t ~= 0 then GetText(t) end
i = 1
end
end
function GetText(b)
local c, d = 1, 1
local bh = {}
local bhc = 1
for i = 1, #b do
if b[i] ~= '' then
d = i
break
end
end
for i = d, #b do
if b[i] == '' then
c = i + 1
break
end
bh[bhc] = b[i]
bhc = bhc + 1
end
if #bh == 0 then return nil end
local tv, te, s
for i = 1, #bh do
if (tv ~= nil) and (te ~= nil) then break end
s = str_match(bh[i], '^([A-Za-z%-]+)[\t ]*:')
if s ~= nil then
s = str_lower(s)
if s == 'content-type' then
tv = bh[i]
if bh[i + 1] ~= nil then
for j = i + 1, #bh do
if str_find(bh[j], '^%s+', 1, false) ~= nil then
tv = tv .. bh[j]
else
break
end
end
end
else
if s == 'content-transfer-encoding' then te = bh[i] end
end
end
end
if str_find(tv, 'text/plain', 1, true) == nil then
if str_find(tv, 'text/html', 1, true) == nil then return nil end
end
d = c
for i = c, #b do
if b[i] ~= '' then
d = i
break
end
end
c = 1
bb = {}
for i = d, #b do
bb[c] = b[i]
c = c + 1
end
if te == nil then te = 'Content-Transfer-Encoding: 8bit' end
local t = GetSingle(bb, tv, te)
sbody = sbody .. '\n' .. t
end
function DecodeBase64(to_decode)
local index_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local padded
if str_find(to_decode, "%s", 1, false) then
padded = str_gsub(to_decode, "%s", "")
else
padded = to_decode
end
local unpadded
if str_find(to_decode, "=", 1, true) then
unpadded = str_gsub(padded, "=", "")
else
unpadded = padded
end
local bit_pattern = ''
local decoded = ''
for i = 1, str_len(unpadded) do
local char = str_sub(to_decode, i, i)
local offset, _ = str_find(index_table, char)
if offset == nil then return "Base64: Invalid character found." end
bit_pattern = bit_pattern .. str_sub(to_binary(offset - 1), 3)
end
for i = 1, str_len(bit_pattern), 8 do
local byte = str_sub(bit_pattern, i, i + 7)
decoded = decoded .. str_char(tonum(byte, 2))
end
local padding_length = str_len(padded) - str_len(unpadded)
if (padding_length == 1 or padding_length == 2) then
decoded = str_sub(decoded, 1, -2)
end
return decoded
end
function to_binary(integer)
local remaining = tonum(integer)
local bin_bits = ''
for i = 7, 0, -1 do
local current_power = 2 ^ i
if remaining >= current_power then
bin_bits = bin_bits .. '1'
remaining = remaining - current_power
else
bin_bits = bin_bits .. '0'
end
end
return bin_bits
end
function DecodeQuotedPrintable(s)
return str_gsub(s, '=([A-F0-9][A-F0-9])',
function(h) return str_char(tonum(h,16)) end)
end
function EncodeToUTF8(s, e)
local l = {
['windows-1250'] = 'cp1250',
['windows-1251'] = 'cp1251',
['windows-1252'] = 'cp1252',
['windows-1253'] = 'cp1253',
['windows-1254'] = 'cp1254',
['windows-1255'] = 'cp1255',
['windows-1256'] = 'cp1256',
['windows-1257'] = 'cp1257',
['windows-1258'] = 'cp1258',
['iso-8859-1'] = 'iso88591',
['iso-8859-2'] = 'iso88592',
['iso-8859-15'] = 'iso885915',
['koi8-r'] = 'koi8',
['mac'] = 'macintosh',
['macintosh'] = 'macintosh'
}
if l[e] ~= nil then return LazUtf8.ConvertEncoding(s, l[e], 'utf8') end
return s
end