This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
smd-translate
executable file
·206 lines (189 loc) · 5.22 KB
/
smd-translate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env lua5.1
--
-- Released under the terms of GPLv3 or at your option any later version.
-- No warranties.
-- Copyright Enrico Tassi <[email protected]>
-- 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['nodots2'] = function(dir)
if dir == 'LR' then
local p_mlc = '^'..escape(mailbox_local)..'/(.*)/(cur)'
local p_mln = '^'..escape(mailbox_local)..'/(.*)/(new)'
local p_mlt = '^'..escape(mailbox_local)..'/(.*)/(tmp)'
local mlc = mailbox_local..'/cur'
local mln = mailbox_local..'/new'
local mlt = mailbox_local..'/tmp'
local p_ml = '^'..escape(mailbox_local)..'/'
local mr = mailbox_remote..'/'
for l in io.stdin:lines() do
if l == mlc or l == mln or l == mlt then
print((l:gsub(p_ml,mr)))
else
local f = function(path, last)
return mr..(path:gsub('%.%.',''):gsub('%.','/'))..
'/'..last end
print((l:gsub(p_mlc,f):gsub(p_mln,f):gsub(p_mlt,f)))
end
end
else
local p_mrc = '^'..escape(mailbox_remote)..'/(.*)/(cur)'
local p_mrn = '^'..escape(mailbox_remote)..'/(.*)/(new)'
local p_mrt = '^'..escape(mailbox_remote)..'/(.*)/(tmp)'
local mrc = mailbox_remote..'/cur'
local mrn = mailbox_remote..'/new'
local mrt = mailbox_remote..'/tmp'
local p_mr = '^'..escape(mailbox_remote)..'/'
local ml = mailbox_local..'/'
for l in io.stdin:lines() do
if l == mrc or l == mrn or l == mrt then
print((l:gsub(p_mr,ml)))
else
local f = function(cap, last)
return ml..'..'..(cap:gsub('^//+','/'):gsub('/','.'))..
'/'..last end
print((l:gsub(p_mrc,f):gsub(p_mrn,f):gsub(p_mrt,f)))
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: