No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smd-translate 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env lua5.1
  2. --
  3. -- Released under the terms of GPLv3 or at your option any later version.
  4. -- No warranties.
  5. -- Copyright Enrico Tassi <gares@fettunta.org>
  6. -- utils
  7. function normalize(x)
  8. return x:gsub("^['\"]",''):gsub("['\"]$",''):gsub("/$",'')
  9. end
  10. function escape(x)
  11. return x:gsub('[]%%%.-]',function(x) return '%'..x end)
  12. end
  13. local orig_print = print
  14. function print(...)
  15. orig_print(...)
  16. io.stdout:flush()
  17. end
  18. -- argument parsing
  19. local mode, dir, endpoint
  20. mode = 'default'
  21. local i = 1
  22. while i <= select('#',...) do
  23. local cur = select(i,...)
  24. if cur == '-m' then
  25. i = i + 1
  26. mode = select(i,...)
  27. elseif cur == '-d' then
  28. i = i + 1
  29. dir = select(i,...)
  30. else
  31. if endpoint ~= nil then
  32. print 'ERROR'
  33. print 'smd-translate: too many arguments'
  34. os.exit(1)
  35. end
  36. endpoint = cur
  37. end
  38. i = i + 1
  39. end
  40. -- argument checking
  41. if endpoint == nil then
  42. print 'ERROR'
  43. print('smd-translate: No endpoint specified')
  44. os.exit(1)
  45. end
  46. if dir ~= 'LR' and dir ~= 'RL' then
  47. print 'ERROR'
  48. print('smd-translate: Direction '..(dir or 'nil')..
  49. ' passed with -d is not LR nor R')
  50. os.exit(1)
  51. end
  52. -- config file parsing
  53. conf=os.getenv'HOME'..'/.smd/config.'..endpoint
  54. conf_f = io.open(conf)
  55. if conf_f == nil then
  56. print 'ERROR'
  57. print('smd-translate: No configuration file for endpoint ' ..
  58. (endpoint or 'nil'))
  59. os.exit(1)
  60. end
  61. local conf_tbl = {}
  62. for l in conf_f:lines() do
  63. -- this way we skip comments
  64. local k,v = l:match('^%s*(%S+)%s*=%s*(%S+)')
  65. if k ~= nil and v ~= nil then
  66. conf_tbl[k]=v
  67. end
  68. end
  69. conf_f:close()
  70. mailbox_local = conf_tbl['MAILBOX_LOCAL'] or conf_tbl['MAILBOX']
  71. mailbox_local = normalize(mailbox_local)
  72. mailbox_remote = conf_tbl['MAILBOX_REMOTE'] or conf_tbl['MAILBOX']
  73. mailbox_remote = normalize(mailbox_remote)
  74. -- modes
  75. modes = {}
  76. modes['oimap-dovecot'] = function(dir)
  77. if dir == 'LR' then
  78. local l_ml = '^'..escape(mailbox_local)..'/'
  79. local r_ml_dot = mailbox_remote..'/.'
  80. local r_ml = mailbox_remote..'/'
  81. local mc = mailbox_local..'/cur'
  82. local mn = mailbox_local..'/new'
  83. local mt = mailbox_local..'/tmp'
  84. for l in io.stdin:lines() do
  85. if l == mc or l == mn or l == mt then
  86. print((l:gsub(l_ml,r_ml)))
  87. else
  88. print((l:gsub(l_ml,r_ml_dot)))
  89. end
  90. end
  91. else
  92. local e_mr = '^'..escape(mailbox_remote)
  93. local m_ml = '^'..escape(mailbox_local)..'/%.'
  94. local dot_ml = mailbox_local..'/'
  95. for l in io.stdin:lines() do
  96. print((l:gsub(e_mr,mailbox_local):gsub(m_ml,dot_ml)))
  97. end
  98. end
  99. end
  100. modes['nodots'] = function(dir)
  101. if dir == 'LR' then
  102. local l_ml = '^'..escape(mailbox_local)..'/'
  103. local l_mlc = '^'..escape(mailbox_local)..'/(.*)/(cur)'
  104. local l_mln = '^'..escape(mailbox_local)..'/(.*)/(new)'
  105. local l_mlt = '^'..escape(mailbox_local)..'/(.*)/(tmp)'
  106. local r_ml_dot = mailbox_remote..'/.'
  107. local r_ml = mailbox_remote..'/'
  108. local mc = mailbox_local..'/cur'
  109. local mn = mailbox_local..'/new'
  110. local mt = mailbox_local..'/tmp'
  111. for l in io.stdin:lines() do
  112. if l == mc or l == mn or l == mt then
  113. print((l:gsub(l_ml,r_ml)))
  114. else
  115. local f = function(path, last)
  116. return r_ml_dot..(path:gsub('/','.'))..'/'..last end
  117. print((l:gsub(l_mlc,f):gsub(l_mln,f):gsub(l_mlt,f)))
  118. end
  119. end
  120. else
  121. local e_mr = '^'..escape(mailbox_remote)..'(/.*)'
  122. local dot_ml = mailbox_local
  123. for l in io.stdin:lines() do
  124. print((l:gsub(e_mr,function(cap)
  125. return dot_ml..(cap:gsub('%.','/'):gsub('^//','/')) end)))
  126. end
  127. end
  128. end
  129. modes['move'] = function(dir)
  130. if dir == 'LR' then
  131. local l_m = '^'..escape(mailbox_local)..'/'
  132. local r_m = mailbox_remote..'/'
  133. for l in io.stdin:lines() do
  134. print((l:gsub(l_m,r_m)))
  135. end
  136. else
  137. local r_m = '^'..escape(mailbox_remote)..'/'
  138. local l_m = mailbox_local..'/'
  139. for l in io.stdin:lines() do
  140. print((l:gsub(r_m,l_m)))
  141. end
  142. end
  143. end
  144. modes['cat'] = function(_) for l in io.stdin:lines() do print(l) end end
  145. modes['default'] = modes['oimap-dovecot']
  146. -- run
  147. modes[mode](dir, f,s,init)
  148. -- vim:set ts=4: