My dotfiles. Period.

common.lua 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. os = require "os"
  2. socket = require "socket"
  3. ------------------------------------------------------------------------------
  4. -- config and handling --
  5. ------------------------------------------------------------------------------
  6. function shortname()
  7. --
  8. -- Short hostname
  9. --
  10. local long = socket.dns.gethostname()
  11. return string.gmatch(long, '[^.]+')()
  12. end
  13. function file_exists(name)
  14. --
  15. -- True if file *name* exists
  16. --
  17. local f=io.open(name,"r")
  18. if f~=nil then io.close(f) return true else return false end
  19. end
  20. function init_host()
  21. --
  22. -- Look for action handler in basepaths; do first one
  23. --
  24. local init = imapdomo.dirs.host .. "/init.lua"
  25. if file_exists(init) then
  26. dofile(init)
  27. return
  28. end
  29. end
  30. function handle(action, basepaths)
  31. --
  32. -- Look for action handler in basepaths; do first one
  33. --
  34. local valid = {
  35. newmail = true,
  36. rewind = true,
  37. cleanup = true,
  38. migrate = true
  39. }
  40. if not valid[action] then
  41. error("invalid action: " .. action)
  42. return nil
  43. end
  44. init_host()
  45. for k,v in ipairs({imapdomo.dirs.host, imapdomo.dirs.default}) do
  46. attempt = v .. "/handlers/" .. action .. ".lua"
  47. if file_exists(attempt) then
  48. dofile(attempt)
  49. return
  50. end
  51. end
  52. end
  53. ------------------------------------------------------------------------------
  54. -- mail getters --
  55. ------------------------------------------------------------------------------
  56. function get_queue(acct, mbox)
  57. --
  58. -- Get queue from *mbox* from *acct* or nil (no messages)
  59. --
  60. -- If mbox is not specified, "FILTER_QUEUE" is used
  61. --
  62. mbox = mbox or "FILTER_QUEUE"
  63. local exist, unread, unseen, uidnext = acct[mbox]:check_status()
  64. if exist > 0 then
  65. return acct[mbox]:select_all()
  66. end
  67. return nil
  68. end
  69. function _notifirc1(subj, from, body)
  70. --
  71. -- notify about message using (pre-configured) notifirc
  72. --
  73. local fd = assert(io.popen('notifirc -c mail -f - "message preview:"', "w"))
  74. local fmt = "> %s\n> %s\n> BODY: %s"
  75. fd:write(fmt:format(subj, from, body))
  76. fd:close()
  77. end
  78. function notifirc_all(seq)
  79. --
  80. -- Send notifications about all messages in *seq*
  81. --
  82. for _, mesg in ipairs(seq) do
  83. mbox, uid = table.unpack(mesg)
  84. subj = mbox[uid]:fetch_field('Subject')
  85. from = mbox[uid]:fetch_field('From')
  86. body = mbox[uid]:fetch_body()
  87. _notifirc1(subj, from, body)
  88. end
  89. end
  90. function _partinf_compare(a, b)
  91. if not type(a) == type(b) then
  92. return false
  93. end
  94. if type(a) == 'number' then
  95. return a == b
  96. elseif type(a) == 'string' then
  97. return a:lower() == b:lower()
  98. end
  99. end
  100. function has_part_like(query, structure)
  101. --
  102. -- True if structure has MIME part matching *query*
  103. --
  104. if structure == nil then
  105. return false
  106. end
  107. for partid, partinf in pairs(structure) do
  108. local part_answer = true
  109. -- check all query parts
  110. for qkey, qvalue in pairs(query) do
  111. value = partinf[qkey]
  112. if not _partinf_compare(value, qvalue) then
  113. part_answer = false
  114. break
  115. end
  116. end
  117. if part_answer then
  118. return true
  119. end
  120. end
  121. return false
  122. end
  123. function filter_part_like(query, seq)
  124. --
  125. -- Run MIME part query on *seq* sequence of messages
  126. --
  127. result = Set {}
  128. for _, mesg in ipairs(seq) do
  129. mbox, uid = table.unpack(mesg)
  130. structure = mbox[uid]:fetch_structure()
  131. if has_part_like(query, structure) then
  132. table.insert(result, mesg)
  133. end
  134. end
  135. return result
  136. end