imapfilter convenience wrapper

common.lua 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. os = require "os"
  2. ------------------------------------------------------------------------------
  3. -- config and handling --
  4. ------------------------------------------------------------------------------
  5. function file_exists(name)
  6. --
  7. -- True if file *name* exists
  8. --
  9. local f = io.open(name,"r")
  10. if f ~= nil then
  11. io.close(f)
  12. return true
  13. else
  14. return false
  15. end
  16. end
  17. function do_if_exists(filename)
  18. --
  19. -- Do file from home, if exists
  20. --
  21. local file = os.getenv("IMAPFILTER_HOME") .. "/" .. filename
  22. if file_exists(file) then
  23. dofile(file)
  24. return
  25. end
  26. end
  27. function handle()
  28. --
  29. -- Handle action if it's valid
  30. --
  31. local action = os.getenv("IMAPDOMO_ACTION")
  32. local file = os.getenv("IMAPFILTER_HOME") .. "/handlers/" .. action .. ".lua"
  33. if file_exists(file) then
  34. do_if_exists("init.lua")
  35. dofile(file)
  36. else
  37. error("no handler for action: " .. action)
  38. return nil
  39. end
  40. end
  41. ------------------------------------------------------------------------------
  42. -- mail getters --
  43. ------------------------------------------------------------------------------
  44. function get_queue(acct, mbox)
  45. --
  46. -- Get queue from *mbox* from *acct* or nil (no messages)
  47. --
  48. -- If mbox is not specified, "FILTER_QUEUE" is used
  49. --
  50. mbox = mbox or "FILTER_QUEUE"
  51. local exist, unread, unseen, uidnext = acct[mbox]:check_status()
  52. if exist > 0 then
  53. return acct[mbox]:select_all()
  54. end
  55. return nil
  56. end
  57. function _notifirc1(subj, from, body)
  58. --
  59. -- notify about message using (pre-configured) notifirc
  60. --
  61. local fd = assert(io.popen('notifirc -c mail -f - "message preview:"', "w"))
  62. local fmt = "> %s\n> %s\n> BODY: %s"
  63. fd:write(fmt:format(subj, from, body))
  64. fd:close()
  65. end
  66. function notifirc_all(seq)
  67. --
  68. -- Send notifications about all messages in *seq*
  69. --
  70. for _, mesg in ipairs(seq) do
  71. mbox, uid = table.unpack(mesg)
  72. subj = mbox[uid]:fetch_field('Subject')
  73. from = mbox[uid]:fetch_field('From')
  74. body = mbox[uid]:fetch_body()
  75. _notifirc1(subj, from, body)
  76. end
  77. end
  78. function _partinf_compare(a, b)
  79. if not type(a) == type(b) then
  80. return false
  81. end
  82. if type(a) == 'number' then
  83. return a == b
  84. elseif type(a) == 'string' then
  85. return a:lower() == b:lower()
  86. end
  87. end
  88. function has_part_like(query, structure)
  89. --
  90. -- True if structure has MIME part matching *query*
  91. --
  92. if structure == nil then
  93. return false
  94. end
  95. for partid, partinf in pairs(structure) do
  96. local part_answer = true
  97. -- check all query parts
  98. for qkey, qvalue in pairs(query) do
  99. value = partinf[qkey]
  100. if not _partinf_compare(value, qvalue) then
  101. part_answer = false
  102. break
  103. end
  104. end
  105. if part_answer then
  106. return true
  107. end
  108. end
  109. return false
  110. end
  111. function save_header(mesg, name)
  112. --
  113. -- Append header from *mesg* to file named *name*
  114. --
  115. -- File will be placed under directory specified by IMAPDOMO_HEADERS
  116. -- environment variable.
  117. --
  118. local dest = os.getenv("IMAPDOMO_HEADERS") .. '/' .. name
  119. local cmd = ('cat >>"%q"'):format(dest)
  120. mbox, uid = table.unpack(mesg)
  121. header = mbox[uid]:fetch_header()
  122. if pipe_to(cmd, header) == 0 then
  123. return true
  124. else
  125. return false
  126. end
  127. end
  128. function filter_header_saved(seq, name)
  129. --
  130. -- Save headers from sequence
  131. --
  132. -- Append header of each message in sequence *seq* to file names
  133. -- *name* and return new sequence with those messages where save was
  134. -- successful.
  135. --
  136. result = Set {}
  137. for _, mesg in ipairs(seq) do
  138. if save_header(mesg, name) then
  139. table.insert(result, mesg)
  140. end
  141. end
  142. return result
  143. end
  144. function filter_part_like(query, seq)
  145. --
  146. -- Run MIME part query on *seq* sequence of messages
  147. --
  148. result = Set {}
  149. for _, mesg in ipairs(seq) do
  150. mbox, uid = table.unpack(mesg)
  151. structure = mbox[uid]:fetch_structure()
  152. if has_part_like(query, structure) then
  153. table.insert(result, mesg)
  154. end
  155. end
  156. return result
  157. end