It's a type of Planche
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

purestorage_imapfilter.cfg 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. options.subscribe = true
  2. options.keepalive = 10
  3. options.expunge = true
  4. options.info = false
  5. options.certificates = false
  6. -- Global variables
  7. accounts = {}
  8. accounts_fetched = 0
  9. fetch_accounts_every = 3600
  10. filter_every = 30
  11. MONTHS={Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12}
  12. DATE_FORMAT = "(%d+)-(%a+)-(%d+) (%d+):(%d+):(%d+)"
  13. -- Split string
  14. function split(str, delim)
  15. local result,pat,lastPos = {},"(.-)" .. delim .. "()",1
  16. for part, pos in string.gmatch(str, pat) do
  17. table.insert(result, part); lastPos = pos
  18. end
  19. table.insert(result, string.sub(str, lastPos))
  20. return result
  21. end
  22. -- Table size
  23. function size(tbl)
  24. tbl_length = 0
  25. for k, v in ipairs(tbl) do
  26. tbl_length = tbl_length + 1
  27. end
  28. return tbl_length
  29. end
  30. -- Move to Gmail Trash with logging
  31. function move_to_trash(imap, messages)
  32. for _, msg in ipairs(messages) do
  33. mbox, uid = table.unpack(msg)
  34. print('IMAPFilter ' .. mbox[uid]:fetch_field('From') .. ' ' .. mbox[uid]:fetch_field('To') ..
  35. ' ' .. mbox[uid]:fetch_field('Subject') .. ' ' .. mbox[uid]:fetch_date())
  36. end
  37. messages:move_messages(imap['[Gmail]/Trash'])
  38. end
  39. -- IMAP Date string to seconds before now
  40. function seconds_old(date_string)
  41. local day, mon, year, hour, min, sec = date_string:match(DATE_FORMAT)
  42. local time_stamp = os.time({day=day,month=MONTHS[mon],year=year,hour=hour,min=min,sec=sec})
  43. return os.difftime(os.time(), time_stamp)
  44. end
  45. function fetch_accounts()
  46. mysql_command = 'mysql -BNe "select SERVERNAME, smtp_port, mailusername, mailpassword from mailserver where protocol like \'imap%\'"'
  47. status, output = pipe_from(mysql_command)
  48. accounts = {}
  49. for _, account in ipairs(split(output, "\n")) do
  50. local _, _, server, port, username, password = string.find(account, '([^%s]+)[%s]+([^%s]+)[%s]+([^%s]+)[%s]+([^%s]+)')
  51. if server and port and username and password then
  52. local imap = IMAP {
  53. server = server,
  54. port = tonumber(port),
  55. username = username,
  56. password = password,
  57. ssl = 'tls1.2'
  58. }
  59. -- Get a list of the available mailboxes and folders
  60. local mailboxes, folders = imap:list_all()
  61. table.insert(accounts, imap)
  62. end
  63. end
  64. end
  65. -- Filters
  66. function filter(imap)
  67. imap.INBOX:check_status()
  68. results = imap.INBOX:select_all()
  69. -- * for AND, + for OR, - for NOT, () to group
  70. -- Google Calendar Invites
  71. calendar_notifications = ( results:contain_from("calendar-notification@google.com") +
  72. results:contain_field("Sender", "calendar-notification@google.com") )
  73. if size(calendar_notifications) > 0 then
  74. print('IMAPFilter Deleting Google Calendar Invites')
  75. move_to_trash(imap, calendar_notifications)
  76. end
  77. -- Too Old
  78. too_old = ( results:is_older(5) )
  79. if size(too_old) > 0 then
  80. print('IMAPFilter Deleting Too Old')
  81. move_to_trash(imap, too_old)
  82. end
  83. -- Mailer Daemon
  84. mailer_daemon = ( results:contain_from("mailer-daemon@.*") +
  85. results:contain_field("Sender", "mailer-daemon@.*") )
  86. if size(mailer_daemon) > 0 then
  87. print('IMAPFilter Deleting Mailer Daemon Emails')
  88. move_to_trash(imap, mailer_daemon)
  89. end
  90. -- JIRA Trouble Parsing
  91. seen_msgs = ( results:is_seen() )
  92. jira_unable_to_parse = Set {}
  93. if size(seen_msgs) > 0 then
  94. for _, msg in ipairs(seen_msgs) do
  95. mbox, uid = table.unpack(msg)
  96. if seconds_old(mbox[uid]:fetch_date()) > 900 then
  97. table.insert(jira_unable_to_parse, msg)
  98. end
  99. end
  100. end
  101. if size(jira_unable_to_parse) > 0 then
  102. print('IMAPFilter Deleting Mail JIRA Cannot Parse')
  103. move_to_trash(imap, jira_unable_to_parse)
  104. end
  105. -- done
  106. end
  107. function main()
  108. print("IMAPFilter Running at " .. os.date("%Y-%m-%d %H:%M:%S"))
  109. -- Fetch accounts once per hour from MySQL
  110. if os.time() - accounts_fetched > fetch_accounts_every then
  111. fetch_accounts()
  112. end
  113. -- Loop through all accounts and run filtering on them
  114. for _, imap in ipairs(accounts) do
  115. filter(imap)
  116. end
  117. -- print('---------')
  118. end
  119. -- Two methods for running in loop
  120. -- Only uncomment one at a time
  121. -- become_daemon forks into background and calls a function every x seconds
  122. -- become_daemon(filter_every, main)
  123. -- While true loop runs in forground
  124. -- while true do
  125. -- main()
  126. -- Lua has no built in sleep function
  127. -- os.execute('sleep ' .. filter_every)
  128. -- end
  129. -- Run once and exit
  130. main()