Repository navigation
Expand file tree
/
Copy pathotp_code.lua
More file actions
111 lines (99 loc) · 3.92 KB
/
Copy pathotp_code.lua
File metadata and controls
111 lines (99 loc) · 3.92 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
-- otp_code.lua
-- Spots one-time passcodes in sign-in and verification emails and pins them
-- in a card at the top of the message, so you never have to hunt for them.
-- Also shows the code in a notification when it arrives in the subject line.
local matcha = require("matcha")
local cfg = matcha.settings({
notify = {
type = "boolean",
default = true,
label = "Notify on arrival",
description = "Show a notification when a new email's subject contains a code.",
},
})
-- Words that announce a code, matched as whole words ("verif" as a prefix).
-- Weak ones ("code review #1234") only count with a 6-digit or grouped code.
local STRONG = { passcode = true, otp = true, ["2fa"] = true, verif = true }
local WEAK = { code = true, pin = true, password = true }
local function plain(raw)
local s = raw:gsub("<[Ss][Tt][Yy][Ll][Ee].-</[Ss][Tt][Yy][Ll][Ee]>", " ")
s = s:gsub("<[^>]*>", " ")
s = s:gsub(" ", " "):gsub("&#%d+;", " "):gsub("&%a+;", " ")
return (s:gsub("%s+", " "))
end
local function is_year(n)
return #n == 4 and (n:sub(1, 2) == "19" or n:sub(1, 2) == "20")
end
local function digits(tok)
return tok and tok.w:match("^%d+$") ~= nil
end
local function sep(text, a, b, class)
return text:sub(a.e, b.s - 1):match("^" .. class .. "$") ~= nil
end
-- Picks the most code-like number among tokens from..to: a grouped code
-- ("123 456", "123-456", but not a phone number) first, then 6 digits, then
-- (only when loose) any 4-8 digit number that is not a year.
local function pick(text, toks, from, to, loose)
for i = from, to - 1 do
local a, b = toks[i], toks[i + 1]
if a.w:match("^%d%d%d$") and b.w:match("^%d%d%d$") and sep(text, a, b, "[ %-]")
and not (digits(toks[i - 1]) and sep(text, toks[i - 1], a, "[ %-%.]"))
and not (digits(toks[i + 2]) and sep(text, b, toks[i + 2], "[ %-%.]")) then
return a.w .. b.w
end
end
local fallback
for i = from, to do
local n = toks[i].w
if n:match("^%d+$") then
if #n == 6 then return n end
if loose and not fallback and #n >= 4 and #n <= 8 and not is_year(n) then fallback = n end
end
end
return fallback
end
local function find_code(text)
local toks = {}
for s, w, e in text:gmatch("()(%w+)()") do
toks[#toks + 1] = { s = s, w = w, e = e }
end
for i, t in ipairs(toks) do
local lw = t.w:lower()
local strong = STRONG[lw] or STRONG[lw:sub(1, 5)]
if strong or WEAK[lw] then
local code = pick(text, toks, math.max(1, i - 6), math.min(#toks, i + 10), strong)
if code then return code end
end
end
end
local function card(code)
local spaced = code:gsub(".", "%0 "):sub(1, -2)
local label = "VERIFICATION CODE"
local inner = math.max(#label, #spaced) + 4
local border = { color = "42" }
local function row(text, opts, len)
return matcha.style("│", border) .. " " .. matcha.style(text, opts)
.. string.rep(" ", inner - 2 - len) .. matcha.style("│", border)
end
return table.concat({
matcha.style("╭" .. string.rep("─", inner) .. "╮", border),
row(label, { color = "245", bold = true }, #label),
row(spaced, { color = "42", bold = true }, #spaced),
matcha.style("╰" .. string.rep("─", inner) .. "╯", border),
}, "\n")
end
matcha.on("email_body_render", function(email, rendered, raw)
local code = find_code(email.subject or "") or find_code(plain(raw))
if not code then return nil end
return card(code) .. "\n\n" .. rendered
end)
matcha.on("email_received", function(email)
if not cfg.notify then return end
local code = find_code(email.subject or "")
if not code then return end
matcha.notify(code, {
title = "Verification code · " .. (email.from or ""),
duration = 10,
kind = "info",
})
end)