-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best way to decode utf8 headers #178
Comments
Addition: I did not try Thank you very much! |
I wrote this helper function:
If there isn't any better way, feel free to close this issue :) |
Your script didn't work for me as I encountered headers with embedded encoded sections, as well as "B" (base64) encodings, so I decided to modify it a bit: function hdr_decode(s)
local i, j = s:lower():find("=?utf-8?q?", 1, true);
if i then
local k, l = s:find("?=", j, true);
local s_ = s
:sub(j+1, k-1)
:gsub("_", " ")
:gsub("=([a-fA-F0-9][a-fA-F0-9])", function(c) return string.char(tonumber(c, 16)) end);
return hdr_decode(s:sub(1, i-1) .. s_ .. s:sub(l+1))
end
i, j = s:lower():find("=?utf-8?b?", 1, true);
if i then
local k, l = s:find("?=", j, true);
local s_ = s:sub(j+1, k-1):gsub("[%w%+/][%w%+/][%w%+/=][%w%+/=]",
function(w)
local digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
local a = digits:find(w:sub(1, 1), 1, true);
local b = digits:find(w:sub(2, 2), 1, true);
local c = digits:find(w:sub(3, 3), 1, true);
local d = digits:find(w:sub(4, 4), 1, true);
return string.char(
(a-1)*4 + math.floor((b-1)/16),
(b-1)%16*16 + math.floor(((c or 1)-1)/4),
((c or 1)-1)%4*64 + ((d or 1)-1)
):sub(1, d and 3 or c and 2 or 1);
end
);
return hdr_decode(s:sub(1, i-1) .. s_ .. s:sub(l+1));
end
return s;
end
print(hdr_decode("From: =?utf-8?b?SGVsbG8sIFdvcmxkIQ==?= <=?utf-8?q?hello=5Fworld=40example=2ecom?=>"));
-- > From: Hello, World! <hello_world@example.com>
os.exit(0); Note: The recursive approach is slow for larger strings, but it should work well enough for e-mail headers. Also, failure will not be graceful if the input is not well-formed. Finally, I'm using Feel free to use / improve further =) |
Thank you both for the helper. I used LadyBoonami code and it worked just fine. Thank you |
First of all: Thank you for a great project!
I implemented a custom sorting mechanism which iterates over every message in a mailbox and uses
:fetch_field
to fetch header, which are then compared to rules. The problem is that some mails have utf8 encoded headers.logs the following (
imapfilter -v
)What would be the best way to retrieve
[Slack] Notifications from the company workspace for March 13th, 2018 at 5:26 PM
instead of the encoded string? Is there any helper function I could use, or could you expose one, if there isn't?The text was updated successfully, but these errors were encountered: