File: smd-translate

package info (click to toggle)
syncmaildir 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,104 kB
  • sloc: ansic: 6,643; sh: 1,780; makefile: 247
file content (160 lines) | stat: -rwxr-xr-x 3,945 bytes parent folder | download | duplicates (7)
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
#!/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: