decode PGP/MIME (may not fully work)
authorThorsten Glaser <tg@mirbsd.org>
Wed, 9 Mar 2011 14:32:32 +0000 (15:32 +0100)
committerThorsten Glaser <tg@mirbsd.org>
Wed, 9 Mar 2011 14:32:32 +0000 (15:32 +0100)
mksh/depgp [new file with mode: 0644]

diff --git a/mksh/depgp b/mksh/depgp
new file mode 100644 (file)
index 0000000..642b4db
--- /dev/null
@@ -0,0 +1,113 @@
+#!/bin/mksh
+# $MirOS: contrib/hosted/tg/historic/depgp,v 1.2 2009/09/08 17:01:51 tg Exp $
+#-
+# Copyright (c) 2008
+#      Thorsten Glaser <tg@mirbsd.org>
+#
+# Provided that these terms and disclaimer and all copyright notices
+# are retained or reproduced in an accompanying document, permission
+# is granted to deal in this work without restriction, including un-
+# limited rights to use, publicly perform, distribute, sell, modify,
+# merge, give away, or sublicence.
+#
+# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
+# the utmost extent permitted by applicable law, neither express nor
+# implied; without malicious intent or gross negligence. In no event
+# may a licensor, author or contributor be held liable for indirect,
+# direct, other damage, loss, or other issues arising in any way out
+# of dealing in the work, even if advised of the possibility of such
+# damage or existence of a defect, except proven that it results out
+# of said person's immediate fault when using the work as intended.
+#-
+# Decodes PGP/MIME (acts as MDA)
+
+nl='
+'
+if [[ $1 = i ]]; then
+       shift
+else
+       i='--batch --no-tty'
+fi
+T=$(mktemp -d /tmp/depgp.XXXXXXXXXX) || exit 254
+trap "cd /; rm -rf $T; exit 0" 0
+trap "cd /; rm -rf $T; exit 1" 1 2 3 5 13 15
+cd $T
+chmod 0700 .
+(mkdir d
+#sed 's/\r$//' | mksh /usr/mpkg/bin/mbmf filter >t
+sed 's/\r$//' >t
+sed '/^$/,$d' <t >h
+cd d
+if grep -qi '^Content-Type:.*multipart/' ../h; then
+       munpack -t <../t
+elif grep -qi 'Content-Transfer-Encoding:.*QUOTED-PRINTABLE' ../h; then
+       sed '1,/^$/d' <../t | \
+           perl -pe 's/=(\n|[a-fA-F0-9]{2})/$1 eq "\n" ? "" : pack("C",hex($1))/eg' >t
+else
+       sed '1,/^$/d' <../t >t
+fi
+for a in $(fgrep -l -- '-----BEGIN PGP' *); do
+       touch $T/have
+       s=
+       st=0
+       sed 's/\r$//' <$a |&
+       while IFS= read -pr line; do
+               if [[ $st$line == @(0-----BEGIN PGP MESSAGE)* ]]; then
+                       st=1
+                       s=$line
+               elif [[ $st = 1 ]]; then
+                       s=$s$nl$line
+                       if [[ $line == @(-----END PGP MESSAGE)* ]]; then
+                               gpg $i --auto-key-locate keyserver <<<"$s"
+                               s=
+                               st=0
+                       fi
+               else
+                       print -r -- "$line"
+               fi
+       done >$a.dec
+       [[ -n $s ]] && print -r -- "$s" >>$a.dec
+       [[ -s $a.dec ]] && mv -f $a.dec $a
+done
+cd ..) >$T/errmsgs 2>&1
+if [[ -e $T/have ]]; then
+       fn=/home/ork/.etc/Backup/mail.$(date +%s).$$
+       /usr/mpkg/libexec/dmail |&
+       print -p Content-type: text/plain
+       print -p X-Backup-Pathname: $fn
+       pl=
+       while :; do
+               IFS= read -r line
+               if [[ $line = @([        ])* ]]; then
+                       pl=$pl$nl$line
+                       continue
+               fi
+               typeset -l hline=$pl
+               [[ $hline = content-@(type|transfer-encoding):* ]] || \
+                   [[ -z $pl ]] || print -rp -- "$pl"
+               pl=$line
+               [[ -n $pl ]] || break
+       done <t
+       print -p
+       exec 3>&p
+       for file in errmsgs d/*; do
+               print -ru3 "⇒ ${file#d/}"
+               sed 's/\r$//' <$file | if sed '/^$/,$d' <$file | \
+                   grep -qi 'Content-Transfer-Encoding:.*QUOTED-PRINTABLE'; then
+                       perl -pe 's/=(\n|[a-fA-F0-9]{2})/$1 eq "\n" ? "" : pack("C",hex($1))/eg'
+               else
+                       cat
+               fi >&3
+       done
+       exec 3>&-
+       umask 377
+       cat $T/t >$fn
+       chmod 0400 $fn
+       sleep 1
+else
+       sed 's/\r$//' <$T/t | /usr/mpkg/libexec/dmail 2>&1 | \
+           fgrep -v 'delivered to'
+fi
+cd /
+rm -rf $T
+exit 0