My dotfiles. Period.

common.lua 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  39. if not valid[action] then
  40. error("invalid action: " .. action)
  41. return nil
  42. end
  43. init_host()
  44. for k,v in ipairs({imapdomo.dirs.host, imapdomo.dirs.default}) do
  45. attempt = v .. "/handlers/" .. action .. ".lua"
  46. if file_exists(attempt) then
  47. dofile(attempt)
  48. return
  49. end
  50. end
  51. end
  52. ------------------------------------------------------------------------------
  53. -- mail getters --
  54. ------------------------------------------------------------------------------
  55. function get_queue(acct, mbox)
  56. --
  57. -- Get queue from *mbox* from *acct* or nil (no messages)
  58. --
  59. -- If mbox is not specified, "FILTER_QUEUE" is used
  60. --
  61. mbox = mbox or "FILTER_QUEUE"
  62. local exist, unread, unseen, uidnext = acct[mbox]:check_status()
  63. if exist > 0 then
  64. return acct[mbox]:select_all()
  65. end
  66. return nil
  67. end
  68. function _notifirc1(subj, from, body)
  69. --
  70. -- notify about message using (pre-configured) notifirc
  71. --
  72. local fd = assert(io.popen('notifirc -c mail -f - "message preview:"', "w"))
  73. local fmt = "> %s\n> %s\n> BODY: %s"
  74. fd:write(fmt:format(subj, from, body))
  75. fd:close()
  76. end
  77. function notifirc_all(seq)
  78. --
  79. -- Send notifications about all messages in *seq*
  80. --
  81. for _, mesg in ipairs(seq) do
  82. mbox, uid = table.unpack(mesg)
  83. subj = mbox[uid]:fetch_field('Subject')
  84. from = mbox[uid]:fetch_field('From')
  85. body = mbox[uid]:fetch_body()
  86. _notifirc1(subj, from, body)
  87. end
  88. end
  89. function _partinf_compare(a, b)
  90. if not type(a) == type(b) then
  91. return false
  92. end
  93. if type(a) == 'number' then
  94. return a == b
  95. elseif type(a) == 'string' then
  96. return a:lower() == b:lower()
  97. end
  98. end
  99. function has_part_like(query, structure)
  100. --
  101. -- True if structure has MIME part matching *query*
  102. --
  103. if structure == nil then
  104. return false
  105. end
  106. for partid, partinf in pairs(structure) do
  107. local part_answer = true
  108. -- check all query parts
  109. for qkey, qvalue in pairs(query) do
  110. value = partinf[qkey]
  111. if not _partinf_compare(value, qvalue) then
  112. part_answer = false
  113. break
  114. end
  115. end
  116. if part_answer then
  117. return true
  118. end
  119. end
  120. return false
  121. end
  122. function filter_part_like(query, seq)
  123. --
  124. -- Run MIME part query on *seq* sequence of messages
  125. --
  126. result = Set {}
  127. for _, mesg in ipairs(seq) do
  128. mbox, uid = table.unpack(mesg)
  129. structure = mbox[uid]:fetch_structure()
  130. if has_part_like(query, structure) then
  131. table.insert(result, mesg)
  132. end
  133. end
  134. return result
  135. end