123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #!/usr/bin/env lua5.1
- --
- -- Released under the terms of GPLv3 or at your option any later version.
- -- No warranties.
- -- Copyright Enrico Tassi <gares@fettunta.org>
-
- -- utils
- function normalize(x)
- return x:gsub("^['\"]",''):gsub("['\"]$",''):gsub("/$",'')
- end
-
- function escape(x)
- return x:gsub('[]%%%.-]',function(x) return '%'..x end)
- end
-
- local orig_print = print
-
- function print(...)
- orig_print(...)
- io.stdout:flush()
- end
-
- -- argument parsing
- local mode, dir, endpoint
- mode = 'default'
- local i = 1
- while i <= select('#',...) do
- local cur = select(i,...)
- if cur == '-m' then
- i = i + 1
- mode = select(i,...)
- elseif cur == '-d' then
- i = i + 1
- dir = select(i,...)
- else
- if endpoint ~= nil then
- print 'ERROR'
- print 'smd-translate: too many arguments'
- os.exit(1)
- end
- endpoint = cur
- end
- i = i + 1
- end
-
- -- argument checking
- if endpoint == nil then
- print 'ERROR'
- print('smd-translate: No endpoint specified')
- os.exit(1)
- end
- if dir ~= 'LR' and dir ~= 'RL' then
- print 'ERROR'
- print('smd-translate: Direction '..(dir or 'nil')..
- ' passed with -d is not LR nor R')
- os.exit(1)
- end
-
- -- config file parsing
- conf=os.getenv'HOME'..'/.smd/config.'..endpoint
- conf_f = io.open(conf)
- if conf_f == nil then
- print 'ERROR'
- print('smd-translate: No configuration file for endpoint ' ..
- (endpoint or 'nil'))
- os.exit(1)
- end
- local conf_tbl = {}
- for l in conf_f:lines() do
- -- this way we skip comments
- local k,v = l:match('^%s*(%S+)%s*=%s*(%S+)')
- if k ~= nil and v ~= nil then
- conf_tbl[k]=v
- end
- end
- conf_f:close()
-
- mailbox_local = conf_tbl['MAILBOX_LOCAL'] or conf_tbl['MAILBOX']
- mailbox_local = normalize(mailbox_local)
- mailbox_remote = conf_tbl['MAILBOX_REMOTE'] or conf_tbl['MAILBOX']
- mailbox_remote = normalize(mailbox_remote)
-
- -- modes
- modes = {}
- modes['oimap-dovecot'] = function(dir)
- if dir == 'LR' then
- local l_ml = '^'..escape(mailbox_local)..'/'
- local r_ml_dot = mailbox_remote..'/.'
- local r_ml = mailbox_remote..'/'
- local mc = mailbox_local..'/cur'
- local mn = mailbox_local..'/new'
- local mt = mailbox_local..'/tmp'
- for l in io.stdin:lines() do
- if l == mc or l == mn or l == mt then
- print((l:gsub(l_ml,r_ml)))
- else
- print((l:gsub(l_ml,r_ml_dot)))
- end
- end
- else
- local e_mr = '^'..escape(mailbox_remote)
- local m_ml = '^'..escape(mailbox_local)..'/%.'
- local dot_ml = mailbox_local..'/'
- for l in io.stdin:lines() do
- print((l:gsub(e_mr,mailbox_local):gsub(m_ml,dot_ml)))
- end
- end
- end
- modes['nodots'] = function(dir)
- if dir == 'LR' then
- local l_ml = '^'..escape(mailbox_local)..'/'
- local l_mlc = '^'..escape(mailbox_local)..'/(.*)/(cur)'
- local l_mln = '^'..escape(mailbox_local)..'/(.*)/(new)'
- local l_mlt = '^'..escape(mailbox_local)..'/(.*)/(tmp)'
- local r_ml_dot = mailbox_remote..'/.'
- local r_ml = mailbox_remote..'/'
- local mc = mailbox_local..'/cur'
- local mn = mailbox_local..'/new'
- local mt = mailbox_local..'/tmp'
- for l in io.stdin:lines() do
- if l == mc or l == mn or l == mt then
- print((l:gsub(l_ml,r_ml)))
- else
- local f = function(path, last)
- return r_ml_dot..(path:gsub('/','.'))..'/'..last end
- print((l:gsub(l_mlc,f):gsub(l_mln,f):gsub(l_mlt,f)))
- end
- end
- else
- local e_mr = '^'..escape(mailbox_remote)..'(/.*)'
- local dot_ml = mailbox_local
- for l in io.stdin:lines() do
- print((l:gsub(e_mr,function(cap)
- return dot_ml..(cap:gsub('%.','/'):gsub('^//','/')) end)))
- end
- end
-
- end
- modes['move'] = function(dir)
- if dir == 'LR' then
- local l_m = '^'..escape(mailbox_local)..'/'
- local r_m = mailbox_remote..'/'
- for l in io.stdin:lines() do
- print((l:gsub(l_m,r_m)))
- end
- else
- local r_m = '^'..escape(mailbox_remote)..'/'
- local l_m = mailbox_local..'/'
- for l in io.stdin:lines() do
- print((l:gsub(r_m,l_m)))
- end
- end
- end
- modes['cat'] = function(_) for l in io.stdin:lines() do print(l) end end
- modes['default'] = modes['oimap-dovecot']
-
- -- run
- modes[mode](dir, f,s,init)
-
- -- vim:set ts=4:
|