My configuration files for Debian/Ubuntu applications
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

95 linhas
3.1 KiB

  1. --[[
  2. Licensed under GNU General Public License v2
  3. * (c) 2013, Luca CPZ
  4. --]]
  5. local helpers = require("lain.helpers")
  6. local naughty = require("naughty")
  7. local wibox = require("wibox")
  8. local awful = require("awful")
  9. local string = string
  10. local type = type
  11. local tonumber = tonumber
  12. -- Mail IMAP check
  13. -- lain.widget.imap
  14. local function factory(args)
  15. args = args or {}
  16. local imap = { widget = args.widget or wibox.widget.textbox() }
  17. local server = args.server
  18. local mail = args.mail
  19. local password = args.password
  20. local port = args.port or 993
  21. local timeout = args.timeout or 60
  22. local pwdtimeout = args.pwdtimeout or 10
  23. local is_plain = args.is_plain or false
  24. local followtag = args.followtag or false
  25. local notify = args.notify or "on"
  26. local settings = args.settings or function() end
  27. local head_command = "curl --connect-timeout 3 -fsm 3"
  28. local request = "-X 'STATUS INBOX (MESSAGES RECENT UNSEEN)'"
  29. if not server or not mail or not password then return end
  30. mail_notification_preset = {
  31. icon = helpers.icons_dir .. "mail.png",
  32. position = "top_left"
  33. }
  34. helpers.set_map(mail, 0)
  35. if not is_plain then
  36. if type(password) == "string" or type(password) == "table" then
  37. helpers.async(password, function(f) password = f:gsub("\n", "") end)
  38. elseif type(password) == "function" then
  39. imap.pwdtimer = helpers.newtimer(mail .. "-password", pwdtimeout, function()
  40. local retrieved_password, try_again = password()
  41. if not try_again then
  42. imap.pwdtimer:stop() -- stop trying to retrieve
  43. password = retrieved_password or "" -- failsafe
  44. end
  45. end, true, true)
  46. end
  47. end
  48. function imap.update()
  49. -- do not update if the password has not been retrieved yet
  50. if type(password) ~= "string" then return end
  51. local curl = string.format("%s --url imaps://%s:%s/INBOX -u %s:'%s' %s -k",
  52. head_command, server, port, mail, password, request)
  53. helpers.async(curl, function(f)
  54. imap_now = { ["MESSAGES"] = 0, ["RECENT"] = 0, ["UNSEEN"] = 0 }
  55. for s,d in f:gmatch("(%w+)%s+(%d+)") do imap_now[s] = tonumber(d) end
  56. mailcount = imap_now["UNSEEN"] -- backwards compatibility
  57. widget = imap.widget
  58. settings()
  59. if notify == "on" and mailcount and mailcount >= 1 and mailcount > helpers.get_map(mail) then
  60. if followtag then mail_notification_preset.screen = awful.screen.focused() end
  61. naughty.notify {
  62. preset = mail_notification_preset,
  63. text = string.format("%s has <b>%d</b> new message%s", mail, mailcount, mailcount == 1 and "" or "s")
  64. }
  65. end
  66. helpers.set_map(mail, imap_now["UNSEEN"])
  67. end)
  68. end
  69. imap.timer = helpers.newtimer(mail, timeout, imap.update, true, true)
  70. return imap
  71. end
  72. return factory