File: smd-common

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 (612 lines) | stat: -rw-r--r-- 15,475 bytes parent folder | download
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# Released under the terms of GPLv3 or at your option any later version.
# No warranties.
# Copyright Enrico Tassi <gares@fettunta.org>

# Common stuff for smd-pull and smd-push
# Convention:
# - uppercase variables are global
# - lowercase variables are local
# - function arguments are documented assigning to local variable with
#   a decent name positional arguments

### Housekeeping ###

__TOREMOVE=""
__TOKILL=""

atexit_rm() {
	local path="$1"
	__TOREMOVE="$__TOREMOVE $path"
}

atexit_kill() {
	local pid="$1"
	__TOKILL="$__TOKILL $pid"
}

gc_mktemp() {
	local tmp=`mktemp -q /tmp/smd.XXXXXXXXXX`
	atexit_rm $tmp
	RC="$tmp"
}

__cleanup() {
	rm -f $__TOREMOVE
	for p in $__TOKILL; do
		kill $p 2>/dev/null || true
	done
}

trap __cleanup "EXIT"

### Variables setup and sanity checks ###

assert_executable() {
	if type $1 >/dev/null; then
		:
	else
		echo $1 not found, please install it or fix the paths
		echo PATH=$PATH
		echo type $1: `type $1`
		exit 1
	fi
}

init() {
	if [ `echo $PREFIX | cut -c -1` = "@" ]; then
		SMDSERVER=./smd-server
		SMDCLIENT=./smd-client
		MDDIFF=./mddiff
		# in development mode we assume that on the remote host
		# the software is installed such that binaries are in $PATH
		REMOTESMDSERVER=smd-server
		REMOTESMDCLIENT=smd-client
	else
		SMDSERVER=$PREFIX/bin/smd-server
		SMDCLIENT=$PREFIX/bin/smd-client
		MDDIFF=$PREFIX/bin/mddiff
		REMOTESMDSERVER="$SMDSERVER"
		REMOTESMDCLIENT="$SMDCLIENT"
	fi

	H=$HOME
	CONFDIR=$H/.smd
	LOCKFILE=$CONFDIR/lock
	SHOWTAGS=0
	VERBOSE=0
	DRYRUN=0
	TEMPLATE_ONLY=0
	CONFFILE=""
	WORKAREA=$CONFDIR/workarea
	REPNAME=default
	CHILDSARGS=
	REMOTEEXCLUDE=
	LOCALEXCLUDE=
	SMDCLIENTOPTS=
	SMDSERVEROPTS=
	RENAMEDB=.smd/rename-db

	# default values for the configuration file
	DEBUG=false

	# external programs
	SSH="@SSH@"
	if [ `echo "$SSH" | cut -c -1` = "@" ]; then
		SSH=ssh
		echo "`basename $0` not installed, assuming secure shell client is $SSH"
	fi
	SED="@SED@"
	if [ `echo "$SED" | cut -c -1` = "@" ]; then
		SED=sed
		echo "`basename $0` not installed, assuming stream editor is $SED"
	fi

	# sanity checks for required binaries
	assert_executable $SED
	assert_executable $SSH
	assert_executable $MDDIFF
	assert_executable $SMDSERVER
	assert_executable $SMDCLIENT

	# setup of confdir
	$MDDIFF --mkdir-p $CONFDIR/
	$MDDIFF --mkdir-p $CONFDIR/log
	$MDDIFF --mkdir-p $CONFDIR/fifo
	$MDDIFF --mkdir-p $CONFDIR/hooks
	cat > $CONFDIR/hooks/README <<-EOT
	From version 0.9.14, smd-push and smd-pull can run user defined
	hooks before and after doing their job.
	
	Sample hooks are available in the source tarball under sample-hooks/.
	The invocation of hooks is documented in the main README file.
	EOT
	$MDDIFF --mkdir-p $CONFDIR/hooks/pre-pull.d
	$MDDIFF --mkdir-p $CONFDIR/hooks/pre-push.d
	$MDDIFF --mkdir-p $CONFDIR/hooks/post-pull.d
	$MDDIFF --mkdir-p $CONFDIR/hooks/post-push.d
	CRUFT=`find $CONFDIR/workarea ! -type d -a ! -type l 2> /dev/null || true`
	if [ ! -z "$CRUFT" ]; then
		echo "Some files are left in $CONFDIR/workarea"
		echo "This is an internal error. Please report this inconvenience"
		echo "and examine the content of these files, they may be of value."
		echo
		echo $CRUFT
		exit 1
	fi
	rm -rf $CONFDIR/workarea
	$MDDIFF --mkdir-p $CONFDIR/workarea
}

is_absolute() {
	case "$1" in
		/*) return 0 ;;
		*)	return 1 ;;
	esac
}

resolve_translator(){
local T=${1%% *}
if [ ! -z "$T" ]; then
	if is_absolute "$1"; then
		RC="$1"
	elif `type "$HOME/$T" >/dev/null 2>&1`; then
		RC="$HOME/$1"
	elif `type "$T" >/dev/null 2>&1`; then
		RC="$1"
	else
		echo "Unable to find the given translator: $T"
		echo "It is not an absolute path"
		echo "It is not in \$HOME=$HOME"
		echo "It is not in \$PATH=$PATH"
		if [ $showtags = 1 ]; then
			echo "$REPNAME: $localprog@$localhost: TAGS: error::context(conf) probable-cause(translator-not-found) human-intervention(necessary)"
		fi
		exit 1
	fi
else
	RC="cat"
fi
}

setup_workarea(){
	gc_mktemp
	local TMP_FIND="$RC"
	gc_mktemp
	local TMP_FIND_ERR="$RC"
	$MDDIFF $LOCALEXCLUDE -l $MAILBOX_LOCAL >$TMP_FIND 2>$TMP_FIND_ERR
	if [ ! $? -eq 0 ]; then
		echo "$MDDIFF gave an error while scanning $MAILBOX_LOCAL:"
		cat $TMP_FIND_ERR
		exit 1
	fi

	local FIFO_MKDIR="$CONFDIR/fifo/mkdir"
	[ -p "$FIFO_MKDIR" ] || $MDDIFF --mkfifo "$FIFO_MKDIR"
	local FIFO_MKDIRB="$CONFDIR/fifo/mkdir-back"
	[ -p "$FIFO_MKDIRB" ] || $MDDIFF --mkfifo "$FIFO_MKDIRB"
	$MDDIFF -s "$FIFO_MKDIR" > "$FIFO_MKDIRB" &
	local DIR_MAKER=$!
	atexit_kill $DIR_MAKER
	exec 6<$FIFO_MKDIRB
	exec 9>$FIFO_MKDIR

	gc_mktemp
	local TMP_T="$RC"
	local ERR=0
	eval "$TRANSLATOR_LR" <$TMP_FIND >$TMP_T || ERR=$?
	exec 7<$TMP_FIND
	exec 8<$TMP_T

   	while read M <&7 && read TM <&8; do
		if [ $ERR -eq 1 -o "$TM" = "ERROR" ]; then
			echo "Error: translating $M"
			cat $TMP_T
			if [ $showtags = 1 ]; then
				echo "$REPNAME: $localprog@localhost: TAGS: error::context(workarea) probable-cause(bad-translator) human-intervention(necessary)"
			fi
			exit 1
		fi
		if [ $VERBOSE -eq 1 -a "$M" != "$TM" ]; then
			echo "translating:" $M "->" $TM
		fi
		if echo "$TM" | grep -q -e '\.\.'; then
			echo "Error: the translator returned a path containing .."
			exit 1
		fi
		echo "$HOME/$M" 1>&9
		echo ".smd/workarea/$TM" 1>&9
		read R <&6
		if [ "$R" != "OK" ]; then
			echo "$REPNAME: $localprog@localhost: TAGS: error::context(workarea) probable-cause(mddiff-s-error) human-intervention(necessary)"
			exit 1
		fi
	done

	exec 6<&-
	exec 7<&-
	exec 8<&-
	exec 9>&-

	wait $DIR_MAKER || ERR=1
	if [ $ERR -eq 1 ]; then
		echo "Error: creating symlinks"
		if [ $showtags = 1 ]; then
			echo "$REPNAME: $localprog@localhost: TAGS: error::context(workarea) probable-cause(fail-create-symlink) human-intervention(necessary)"
		fi
		exit 1
	fi
}

### Command line argument parsing ###

parse_args() {
	for arg in "$@"; do
		case $arg in
			-v|--verbose)
				VERBOSE=1
				SHOWTAGS=1
				CHILDSARGS="$CHILDSARGS -v"
			;;
			-s|--show-tags)
				SHOWTAGS=1
			;;
			-t|--template-only)
				TEMPLATE_ONLY=1
			;;
			-d|--dry-run)
				DRYRUN=1
				CHILDSARGS="$CHILDSARGS -d"
				VERBOSE=1
				SHOWTAGS=1
			;;
			-n|--no-delete)
				SMDSERVEROPTS="$SMDSERVEROPTS -n"
			;;
			-*)
				cat <<-EOT
				usage: `basename $0` [options] [endpoint]
				Refer to the man page for `basename $0`
				EOT
				exit 1
			;;
			*)
				REPNAME="$arg"
			;;

		esac
	done

	CONFFILE=$CONFDIR/config.$REPNAME
}

### Confdir setup ###

myfakessh() {
	shift
	cd
	"$@"
}

read_conffile() {
	# backward compatibility code
	if [ ! -f $CONFFILE ] && \
	   [ "$REPNAME" = "default" ] && \
	   [ -f $CONFDIR/config ]; then
		# we import the old conffile
		echo "From version 0.9.4, configuration files are named"
		echo "$CONFDIR/config.\$FOO, where FOO is an optional argument"
		echo "to smd-pull/smd-push. The default value of FOO is 'default'."
		echo "I'm renaming $CONFDIR/config to $CONFFILE."
		mv $CONFDIR/config $CONFFILE
	fi

	if [ ! -f $CONFFILE ]; then
		cat > $CONFFILE <<- EOT
		# No config file found, this is a template. You want to edit it.

		# Host name to be used with ssh as the server (use ~/.ssh/config
		# for extra options). smd-pull will pull from this host, smd-push
		# will push to this host and use it as the id of the remote mailbox.
		#
		# You should create an alias within your ~/.ssh/config like the
		# following on:
		#
		#   Host smd-server-foo
		#     Compression yes
		#     Hostname your.real.server.name
		#     Username you
		#
		SERVERNAME=smd-server-$REPNAME

		# Host name to be used as the client.
		# smd-pull will use this just as an id for the client. If you
		# plan to sync with multiple endpoints, you must use a different
		# client id for any of them, thus a pair localhostname-remotehostname
		# should be used
		#
		CLIENTNAME=`hostname`-$REPNAME

		# The mailbox to sync, in case the path is the same on both hosts.
		# The path MUST be relative to the home directory, use a symlink if
		# the mailbox is not rooted there. If these paths contain spaces,
		# they must be substituted with %20.
		#
		MAILBOX="Mail/"

		# Use different paths on the local and remote hosts
		#
		# Local and remote mailbox may differ in name, as well as their
		# sub directory/folder structure. In that case a translator must be
		# provided. A translator is a program that takes in input, as it
		# first and only argument, a directory name (ending in /cur or /new
		# or /tmp) and prints on stdout its translation. Refer to the
		# smd-config (5) manpage for more infos.
		#
		# MAILBOX_LOCAL="Mail/"
		# MAILBOX_REMOTE="OtherMail/"
		# TRANSLATOR_RL=command
		# TRANSLATOR_LR=command

		# Ignore some paths
		#
		# To exclude some paths from the synchronization you can specify
		# a space separated list of glob(7) expressions. Spaces in these
		# expressions must be replaced with %20.
		#
		# EXCLUDE="Mail/spam Mail/trash Mail/with%20spaces"
		#
		# If the local and remote mailbox differ in name or their
		# sub directory/folder structure you can specify different
		# excluded paths for the two endpoints.
		#
		# EXCLUDE_LOCAL="Mail/spam Mail/trash"
		# EXCLUDE_REMOTE="OtherMail/with%20spaces"

		# Local synchronization
		#
		# If the local and remote mailboxes are on the same host
		# the  following option must be added to the configuration file:
		#
		# SMDCLIENTOPTS=-l

		# Avoid deletions
		#
		# In some cases, usually unidirectional synchronizations, one may want
		# to not propagate deletions. E.g. one keeps a slim working mailbox but
		# pushes to a backup mailbox to save every email.
		# 
		# SMDSERVEROPTS=-n

		# If the local and remote mailboxes are on the same host
		# the  following option must be added to the configuration file:
		#
		# SMDCLIENTOPTS=-l

		# Log client to server and server to client communication.
		#
		# This is useful only for debugging, since all network traffic
		# is dumped, including transmitted mails.
		#
		# DEBUG=true
		EOT
		echo No config file found: created a default one
		echo Please edit it: $CONFFILE
		if [ "$TEMPLATE_ONLY" = 1 ]; then
			exit 0
		else
			exit 1
		fi
	fi

	if [ "$TEMPLATE_ONLY" = 1 ]; then
		exit 0
	fi

	. $CONFFILE

	# sanityze
	MAILBOX="${MAILBOX%%/}"
	MAILBOX_LOCAL="${MAILBOX_LOCAL%%/}"
	MAILBOX_REMOTE="${MAILBOX_REMOTE%%/}"

	# default exclude
	if [ -z "$EXCLUDE_LOCAL" ]; then
			EXCLUDE_LOCAL="$EXCLUDE"
	fi
	if [ -z "$EXCLUDE_REMOTE" ]; then
			EXCLUDE_REMOTE="$EXCLUDE"
	fi
	for e in $EXCLUDE_LOCAL; do
			LOCALEXCLUDE="$LOCALEXCLUDE --exclude $e"
	done
	for e in $EXCLUDE_REMOTE; do
			REMOTEEXCLUDE="$REMOTEEXCLUDE --exclude $e"
	done

	# check for local synchronization
	if [ "$SERVERNAME" = "localhost" ]; then
		if echo  "$SMDCLIENTOPTS" | grep -q -v -e "-l"; then
			echo "SERVERNAME is localhost but SMDCLIENTOPTS is not set."
			echo "Local synchronizations must set SMDCLIENTOPTS."
		else
			# no need to ssh
			SSH=myfakessh
		fi
	fi
}

### Only one instance at a time please ###

check_lockfile() {
	# could be relaxed to non related mailboxes/enpoints, but the test is
	# not straightforward
	if [ -f $LOCKFILE ]; then
		if ps -p `cat $LOCKFILE` 2> /dev/null | grep -E 'smd-(push|pull)'; then
			echo Already running.
			echo If this is not the case, remove $LOCKFILE by hand.
			echo "any: smd-pushpull@localhost: TAGS: error::context(locking) probable-cause(another-instance-is-running) human-intervention(necessary) suggested-actions(run(kill `cat $LOCKFILE`) run(rm $LOCKFILE))"
			exit 1
		else
			echo Found lockfile of a dead instance. Ignored.
		fi
	fi

	echo $$ > $LOCKFILE
	atexit_rm $LOCKFILE
}

### Create all the needed pipes ###

setup_plumbing() {
	CtL=$CONFDIR/fifo/c2l.$REPNAME
	LtC=$CONFDIR/fifo/l2c.$REPNAME
	LtS=$CONFDIR/fifo/l2s.$REPNAME
	StL=$CONFDIR/fifo/s2l.$REPNAME
	PRp=$CONFDIR/fifo/pr.$REPNAME

	[ -p $CtL ] || $MDDIFF --mkfifo $CtL
	[ -p $LtC ] || $MDDIFF --mkfifo $LtC
	[ -p $LtS ] || $MDDIFF --mkfifo $LtS
	[ -p $StL ] || $MDDIFF --mkfifo $StL
	[ -p $PRp ] || $MDDIFF --mkfifo $PRp
}

### Logging ###

mycat() {
	# like cat, but ignores arguments
	cat
}

myreporter() {
	tee -a $1 | grep --line-buffered ^PROGRESS: | sed 's?^PROGRESS: ??'
}

mysilentreporter() {
	cat >> $1
}

setup_logging() {
	CtS=$CONFDIR/log/c2s.$REPNAME.log
	StC=$CONFDIR/log/s2c.$REPNAME.log
	CL=$CONFDIR/log/client.$REPNAME.log
	SL=$CONFDIR/log/server.$REPNAME.log

	MITM=mycat
	if [ "$DEBUG" = "true" ]; then
		MITM=tee
		CHILDSARGS="$CHILDSARGS -v"
	fi

	PROGRESS_REPORTER=mysilentreporter
	if [ $VERBOSE -eq 1 ]; then
		PROGRESS_REPORTER=myreporter
	fi
}

setup_mailboxnames() {
	if [ -z "$MAILBOX" ]; then
		if [ -z "$MAILBOX_LOCAL" -o \
			 -z "$MAILBOX_REMOTE" -o \
			 -z "$TRANSLATOR_RL" -o \
			 -z "$TRANSLATOR_LR" ]; then
			echo "The config file must define MAILBOX xor MAILBOX_LOCAL, MAILBOX_REMOTE, TRANSLATOR_LR and TRANSLATOR_RL"
			exit 1
		fi
	else
		if [ ! -z "$MAILBOX_LOCAL" -o ! -z "$MAILBOX_REMOTE" ]; then
			echo "The config file must define MAILBOX xor MAILBOX_LOCAL, MAILBOX_REMOTE, TRANSLATOR_LR and TRANSLATOR_RL"
			exit 1
		fi
		MAILBOX_LOCAL="$MAILBOX"
		MAILBOX_REMOTE="$MAILBOX"
	fi

	if echo "$MAILBOX_LOCAL $MAILBOX_REMOTE" | grep -q -e '\.\.'; then
		echo "Mailbox names can't contain .."
		exit 1
	fi

	resolve_translator "$TRANSLATOR_RL"
	TRANSLATOR_RL="$RC"
	resolve_translator "$TRANSLATOR_LR"
	TRANSLATOR_LR="$RC"
}

# this could be a system wide post-* hook
report() {
	local exitcode="$1"
	local showtags="$2"
	local currcmd="$3"
	local inversecmd="$4"
	local localprog="$5"
	local remoteprog="$6"
	if [ $VERBOSE -eq 1 ]; then
		grep ^INFO: $SL | $SED 's/^INFO: //'
		grep ^INFO: $CL | $SED 's/^INFO: //'
	fi
	if [ $exitcode = 1 ]; then
		grep ^ERROR $SL \
			| $SED "s/^/$remoteprog: /" \
			| $SED "s/@@INVERSECOMMAND@@/$inversecmd/" \
			| $SED "s/@@ENDPOINT@@/$REPNAME/"
		grep ^ERROR $CL \
			| $SED "s/^/$localprog: /" \
			| $SED "s/@@INVERSECOMMAND@@/$inversecmd/" \
			| $SED "s/@@ENDPOINT@@/$REPNAME/"
		grep ^ssh: $SL \
			| $SED "s/^/$remoteprog: ERROR: /"
	fi
	if [ $showtags = 1 ]; then
		#echo "`date`: $currcmd $SERVERNAME" >> $CL
		grep ^TAGS $SL \
			| $SED "s/^/$REPNAME: $remoteprog@$SERVERNAME: /" \
			| $SED "s/@@INVERSECOMMAND@@/$inversecmd/" \
			| $SED "s/@@ENDPOINT@@/$REPNAME/"
		grep ^TAGS $CL \
			| $SED "s/^/$REPNAME: $localprog@localhost: /" \
			| $SED "s/@@INVERSECOMMAND@@/$inversecmd/" \
			| $SED "s/@@ENDPOINT@@/$REPNAME/"
		if [ `grep ^TAGS $SL|wc -l` = 0 ] && \
		   [ `grep ^TAGS $CL|wc -l` = 0 ]; then
			# it may be that ssh failed to resolve the hostname
			# so we generate a fake tag for it
			cat $SL $CL
			echo "$REPNAME: $remoteprog@$SERVERNAME: TAGS: error::context(ssh) probable-cause(network) human-intervention(avoidable) suggested-actions(retry)"
		fi
	fi
}

### Hooks ###

run_hooks() {
	local dir="$1"
	local when="$2"
	local what="$3"
	local status="$4"
	for h in $dir/hooks/$when-$what.d/*; do
		if [ -x $h ]; then
			$h $when $what $REPNAME $status >> $CL 2>&1
		fi
	done
}

# running server and client with appropriate parameters

run_local_client() {
cd $WORKAREA; $SMDCLIENT $CHILDSARGS $SMDCLIENTOPTS -t "$TRANSLATOR_RL" $CLIENTNAME $MAILBOX_REMOTE 
}

run_local_server() {
cd $WORKAREA; $SMDSERVER $LOCALEXCLUDE $CHILDSARGS $SMDSERVEROPTS $CLIENTNAME $MAILBOX_REMOTE
}

run_remote_server() {
$SSH $SERVERNAME $REMOTESMDSERVER $REMOTEEXCLUDE $CHILDSARGS $SMDSERVEROPTS $CLIENTNAME $MAILBOX_REMOTE
}

run_remote_client() {
$SSH $SERVERNAME $REMOTESMDCLIENT $CHILDSARGS $SMDCLIENTOPTS $CLIENTNAME $MAILBOX_REMOTE
}

# vim:ts=4 filetype=sh: