summaryrefslogtreecommitdiff
path: root/.config/imapfilter/imapfilter-common.lua
blob: 8460bb03046f0b738a1bf292aba82aca8bdd42d7 (plain)
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
-- Utility function to get IMAP username from pass
function get_imap_username(account)
    local str = io.popen('pass institutions/' .. account .. '/id'):read()
    return str;
end

-- Utility function to get IMAP password from pass
function get_imap_password(account)
    local str = io.popen('pass institutions/' .. account .. '/pass'):read()
    return str;
end

-- https://stackoverflow.com/a/11402486
function case_insensitive_pattern(pattern)

  -- find an optional '%' (group 1) followed by any character (group 2)
  local p = pattern:gsub("(%%?)(.)", function(percent, letter)

    if percent ~= "" or not letter:match("%a") then
      -- if the '%' matched, or `letter` is not a letter, return "as is"
      return percent .. letter
    else
      -- else, return a case-insensitive character class of the matched letter
      return string.format("[%s%s]", letter:lower(), letter:upper())
    end

  end)

  return p
end