: ########################################################################## # Shellscript: mailex - extract file from mail # Version : 0.1 (beta) # Project : # Author : Heiner Steven (heiner.steven@odn.de) # Written : 23.01.1995 # Category : Mail # SCCS-Id. : $Id: mailex,v 1.1.1.1 1999/06/15 19:29:05 heiner Exp $ ########################################################################## # Description # Bugs # mailx does not recognize duplicate mails ########################################################################## PN=`basename $0` # program name VER='0.1 (beta)' AWK=awk [ -x /bin/nawk ] && AWK=/bin/nawk : ${USER:=`who am i | cut -d' ' -f1`} : ${MailBox:=${MAIL:-/usr/spool/mail/$USER}} Usage () { echo "$PN - extract ftpmail files from mail, $VER (stv '94) usage: $PN [-s] [-f mail_box] ftp_filename new_filename -l: list ftpmail files in mailbox -s: be silent -f: name of input mail file (default is $MailBox) If no parameter is given all ftpmail-files in the mailbox are listed (like parameter -l)." >&2 exit 1 } Err () { for i do echo "$PN: $i" >&2 done } Fatal () { Err "$@"; exit 1; } Msg () { if [ "$silent" != y ]; then Err "$@"; fi; } # ListFiles (mailbox, ...) ListFiles () { $AWK ' ($0 ~ /^From: /) { while ( getline && $0 !~ /^From / ) { if ( $0 ~ /^Subject:.*\[[0-9][0-9]* of [0-9][0-9]*\]/ ) { # Subject: [001 of 035] murx.bintec.de:/pub/dict/german (get german) n = split ($0, line) nr = substr (line [2], 2) + 0 total = substr (line [4], 1, length (line [4])-1) + 0 if ( split (line [5], tmp, ":") == 2 ) { host = tmp [1] path = tmp [2] n = split (path, tmp, "/") file = tmp [n] } else exit Files [path] = total FCount [path]++ break } } } END { for ( i in Files ) print i, Files [i], FCount [i] } ' "$@" } ExtractFile () { : ${1?} ${2?} $AWK ' BEGIN { Base = "'$Tmp'" } ($0 ~ /^From: /) { while ( getline && $0 !~ /^From / ) { if ( $0 ~ /^Subject: \[[0-9][0-9]* of [0-9][0-9]*\]/ ) { # Subject: [001 of 035] murx.bintec.de:/pub/dict/german (get german) n = split ($0, line) nr = substr (line [2], 2) + 0 # part number ltotal = substr (line [4], 1, length (line [4])-1) + 0 if ( split (line [5], tmp, ":") == 2 ) { host = tmp [1]; path = tmp [2] n = split (path, tmp, "/") file = tmp [n] if ( path != "'"$2"'" ) { file=""; break } } else break fname = Base nr "" nfiles++ total = ltotal if ( "'$silent'" != "y" ) printf "processing ftp mail (%3d/%3d, file %s)\n", nr, total, path | "cat >&2" while ( getline && $0 !~ /^ftpmail-begin/ ) ; while ( getline && $0 !~ /^ftpmail-end/ ) print >> fname close fname } } } END { printf "%d\t%d\t\"%s\"\n", nfiles, total, file } ' "$1" } Tmp=${TMPDIR:=/tmp}/ms$$. trap "rm -f ${Tmp}* > /dev/null 2>&1" 0 trap "exit 2" 1 2 3 15 silent=n list=n File= NewFile= while [ $# -gt 0 ] do case "$1" in -f) MailBox="$2"; shift;; -s) silent=y;; -h) Usage;; -l) list=y;; *) if [ -z "$File" ] then File="$1" elif [ -z "$NewFile" ] then NewFile="$1" else Usage fi;; esac shift done [ -n "$MailBox" -a -r "$MailBox" ] || Fatal "cannot open $MailBox" if [ -z "$File" ] then # list files in mailbox ListFiles $MailBox exit 0 fi [ "$list" = y ] && ListFiles $MailBox eval set -- `ExtractFile "$MailBox" "$File"` n="${1:-0}" [ $n -lt 1 ] && Fatal "File $File not found in mail file $MailBox" total="${2:-0}" Msg "processing $total files" Out="${Tmp}all" > $Out Nr=0 missing=0 while [ $Nr -lt "$total" ] do Nr=`expr $Nr + 1` In="$Tmp$Nr" [ -r "$In" ] || { Msg "WARNING: part $Nr of file $File is missing - ignored" missing=`expr $missing + 1` continue } cat "$In" >> $Out && rm -f $In > /dev/null 2>&1 done # uudecode will extract file with this name: Name=`$AWK '/^begin / { print $3; exit 0; }' < $Out ` uudecode $Out if [ -n "$NewFile" -a "$NewFile" != "$Name" ] then mv $Name $NewFile else NewFile="$Name" : ${NewFile:=$PN.out} fi [ "$missing" -gt 0 ] && Err "file may be corrupted - $missing out of $total parts are missing" Msg "file \"$File\" is written to \"$NewFile\"" exit 0