jaromail

a commandline tool to easily and privately handle your e-mail
git clone git://parazyd.org/jaromail.git
Log | Files | Refs | Submodules | README

commit 44da3fa8606b454070bba8f30dd24027fdd3c738
parent d31991adb9236cba81658d07766175c92b69511a
Author: Jaromil <jaromil@dyne.org>
Date:   Thu,  8 May 2014 11:35:23 +0200

tidying up the locking

Diffstat:
Msrc/jaro | 16++++++----------
Msrc/zlibs/email | 42+++++++++++++++++++++---------------------
Msrc/zlibs/maildirs | 20++++++++++----------
3 files changed, 37 insertions(+), 41 deletions(-)

diff --git a/src/jaro b/src/jaro @@ -282,8 +282,6 @@ GNOMEKEY=0 cleanexit() { func "Clean exit procedures" - sleep 3 - unset name login host protocol port password auth folders accountopt # security check { test "$TMPDIR" = "" } && { @@ -293,22 +291,20 @@ cleanexit() { } # first delete dirs - tmpdirs=`${=find} $TMPDIR/ -maxdepth 1 -type d` + tmpdirs=`${=find} "$TMPDIR" -maxdepth 1 -type d` for d in ${(f)tmpdirs}; do - { test "$d" = "$TMPDIR/" } || { + { test "$d" = "$TMPDIR" } || { func "deleting dir: $d" ${=rm} -r "${d}" } done # then locks, with a warning - llist=`${=find} $TMPDIR/ -maxdepth 1 -type f -name '*.lock'` + llist=`${=find} "$TMPDIR" -maxdepth 1 -type f -name '*.lock'` for l in ${(f)llist}; do lname=`basename ${(s:.lock:)l}` func "cleaning lock for $lname" # skip if in course of unlink - parallel operation, see unlink() - { test -r ${TMPDIR}/${lname}.unlink } && { continue } - pidfile="${TMPDIR}/$lname.pid" if [ -r ${pidfile} ]; then pid=`cat $pidfile` @@ -317,10 +313,10 @@ cleanexit() { else func "forced removal of lock left by unknown pid: $lname" fi - rm -f ${TMPDIR}/${lname}.lock + rm -f "${TMPDIR}/${lname}.lock" # remove the actual file - if [ -r ${TMPDIR}/$lname ]; then + if [ -r "${TMPDIR}/$lname" ]; then func "deleting temp file: ${TMPDIR}/$lname" ${=rm} "${TMPDIR}/$lname" else func "empty lock: file was already removed"; fi @@ -335,7 +331,7 @@ TRAPINT() { error "Caught signal, aborting operations." { test $CLEANEXIT = 1 } && { - error "Forced removal of locks" + func "Forcing removal of locks" cleanexit & } if [ "$DEBUG" = "1" ]; then return 1 diff --git a/src/zlibs/email b/src/zlibs/email @@ -311,22 +311,6 @@ send() { unlock "${MAILDIRS}/outbox" return 1 } - tmp="$TMPDIR/msmtp.$host.$datestamp.$RANDOM" - newlock $tmp - cat <<EOF > $tmp -account default -from ${email} -user ${login} -host ${smtp} -port ${smtp_port} -tls on -tls_starttls on -tls_certcheck off -logfile "${MAILDIRS}/logs/msmtp.log" -auth ${auth} -password ${password} -EOF - for qbody in ${(f)queue_outbox}; do # check if this is an anonymous mail @@ -394,11 +378,29 @@ BEGIN { head=1 } /^Cc:/ { print " . " $0 } /^Subject:/ { print " . " $0 } ' + + tmp="$TMPDIR/msmtp.$host.$datestamp.$RANDOM" + newlock $tmp + cat <<EOF > $tmp +account default +from ${email} +user ${login} +host ${smtp} +port ${smtp_port} +tls on +tls_starttls on +tls_certcheck off +logfile "${MAILDIRS}/logs/msmtp.log" +auth ${auth} +password ${password} +EOF msmtp -C $tmp -t < "${qbody}" res=$? - + unlink $tmp fi + unset password + # evaluate results if [ "$res" != "0" ]; then error "Error sending mail, skipped" @@ -406,15 +408,13 @@ BEGIN { head=1 } notice "Mail sent succesfully" # whitelist those to whom we send mails cat "$qbody" | "$WORKDIR/bin/jaro" -q learn recipient - mv "$qbody" "$MAILDIRS/sent/new/" + cat "$qbody" | deliver sent + { test $? = 0 } && { rm "$qbody" } fi done - unset password unlock "$MAILDIRS/outbox" - unlock $tmp - ${=rm} $tmp return 0 } diff --git a/src/zlibs/maildirs b/src/zlibs/maildirs @@ -131,11 +131,8 @@ rmdupes() { tot=0 typeset -al msgs - { test -r "$formail_cache" } || { - formail_cache="$TMPDIR/filter.rmdupes.$datestamp.$RANDOM" - newlock "$formail_cache" - mycache=1 - } + formail_cache="$TMPDIR/filter.rmdupes.$datestamp.$RANDOM" + newlock "$formail_cache" for folder in ${=@}; do { test -r "$folder" } || { folder="$MAILDIRS/$folder" } @@ -157,14 +154,14 @@ rmdupes() { for m in ${(f)msgs}; do func "formail < $m" # 128MB should be enough ehre? - formail -D 128000000 $formail_cache <"$m" \ + formail -D 128000000 "$formail_cache" <"$m" \ && rm "$m" && c=$(( $c + 1 )) done act "$c duplicates found and deleted" tot=$(( $tot + $c )) done - { test "$mycache" = "1" } && { unlock "$formail_cache" } + unlink "$formail_cache" if [ "$tot" = "0" ]; then act "No duplicates found at all" @@ -189,6 +186,9 @@ merge() { error "Destination is not a maildir: $dst" return 1 } + # merge does not uses deliver() because + # the new-flag and read-flags must be kept intact. + # it will abort on any single error on moving each file. notice "Merging maildir ${src} into ${dst}" c=0 fr=`${=find} ${src}/cur -type f` @@ -216,13 +216,13 @@ merge() { return 1 } done notice "$c mails succesfully moved" + act "Removing source directory ${src}" ${=rm} -r "${src}" + act "Purging duplicates in destination" - formail_cache="$TMPDIR/filter.rmdupes.$datestamp.$RANDOM" - newlock "$formail_cache" rmdupes "${dst}" - unlock "$formail_cache" + act "Done. All mails merged into ${dst}" }