My dotfiles. Period.

functions.lua 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. os = require "os"
  2. socket = require "socket"
  3. function shortname()
  4. local long = socket.dns.gethostname()
  5. return string.gmatch(long, '[^.]+')()
  6. end
  7. function get_queue(mbox)
  8. --TODO: try to get tail
  9. return mbox.FILTER_QUEUE:is_newer(84)
  10. end
  11. function _notifirc1(subj, from, body)
  12. --notify about message using (pre-configured) notifirc
  13. local fd = assert(io.popen('notifirc -f - "message preview:"', "w"))
  14. local fmt = "> %s\n> %s\n> BODY: %s"
  15. fd:write(fmt:format(subj, from, body))
  16. fd:close()
  17. end
  18. function notifirc_all(seq)
  19. for _, mesg in ipairs(seq) do
  20. mbox, uid = table.unpack(mesg)
  21. subj = mbox[uid]:fetch_field('Subject')
  22. from = mbox[uid]:fetch_field('From')
  23. body = mbox[uid]:fetch_body()
  24. _notifirc1(subj, from, body)
  25. end
  26. end
  27. function _partinf_compare(a, b)
  28. if not type(a) == type(b) then
  29. return false
  30. end
  31. if type(a) == 'number' then
  32. return a == b
  33. elseif type(a) == 'string' then
  34. return a:lower() == b:lower()
  35. end
  36. end
  37. function has_part_like(query, structure)
  38. if structure == nil then
  39. return false
  40. end
  41. for partid, partinf in pairs(structure) do
  42. local part_answer = true
  43. -- check all query parts
  44. for qkey, qvalue in pairs(query) do
  45. value = partinf[qkey]
  46. if not _partinf_compare(value, qvalue) then
  47. part_answer = false
  48. break
  49. end
  50. end
  51. if part_answer then
  52. return true
  53. end
  54. end
  55. return false
  56. end
  57. function filter_part_like(query, seq)
  58. result = Set {}
  59. for _, mesg in ipairs(seq) do
  60. mbox, uid = table.unpack(mesg)
  61. structure = mbox[uid]:fetch_structure()
  62. if has_part_like(query, structure) then
  63. table.insert(result, mesg)
  64. end
  65. end
  66. return result
  67. end