FTPMail

Con questo lavoro ho voluto realizzare un'applicazione client-server, specificamente pensata per l'ambiente del D.E.I., che mi permettesse di scaricare un file dalla rete, suddividerlo in parti uguali di una dimensione massima definibile a piacere e ricevere il tutto, una parte alla volta con ricomposizione automatica, sul mio account privato a casa. Il tutto attraverso la posta elettronica.

Il risultato non è stato un grande successo per via delle limitazioni imposte al D.E.I., ma contiene, secondo me, delle interessanti idee realizzate nel linguaggio di procmail.

Per maggior comodità nella sezione Download è disponibile il pacchetto FTPMail.tgz contenente i sorgenti presenti qui sotto.

Lato server

:0
* !^Subject:.*Re:
* !^FROM_DAEMON
* !^X-Loop: $LOGNAME@$HOST
* ^To: +FTPMail-server
* ^Subject: +\/.*
{
   comment="$MATCH"
   LOGFILE=$HOME/tmp/FTPMail-$$.log
   VERBOSE=yes
   LOGABSTRACT=all
   FTPLOGFILE=$HOME/tmp/FTPGet.log
   TRAP="${TRAP:+$TRAP;}							 [ \"\${DEBUG}foo\" != \"foo\" ] &&					 ( echo \"$DEFHEADER\";							   echo \"Subject: debugging file of \\\"$comment\\\"\";		   echo;								   cat $LOGFILE								 ) | $SENDMAIL -oi -t;							 rm -f $LOGFILE $FTPLOGFILE"

   TO=`formail -rtx"To:"`
   DEFHEADER="From: FTPMail-server <$LOGNAME@$HOST>\\nTo:$TO\\nX-Loop: $LOGNAME@$HOST"

   PATH=/nfsd/uns_local/bin:/usr/local/bin:/bin:/usr/bin:$HOME/bin:/nfsd/locmarina/bin
   LD_LIBRARY_PATH=$HOME/lib
   HOMEDIR=/home
   TMPDIR=tmp
   GOTFILE=got.`/usr/xpg4/bin/id -u`
   NEXTPHRASE="send me the next part of"
   a8	     =141	# 'a' in base 8 is 141
   STOREDIRS=`echo "$HOME/$TMPDIR							    $HOME/$TMPDIR							    $HOME/$TMPDIR							    $HOME/$TMPDIR							    $HOMEDIR/mamie/$TMPDIR						    $HOMEDIR/keatch/$TMPDIR"						    $HOMEDIR/mamie/$TMPDIR						    | tr -s [:blank:] ' '`
   
   :0
   * ! $^Subject: +${NEXTPHRASE}.*
   {
	OUTFILE=/tmp/$GOTFILE
	LOCKSLEEP=99
	
	:0:FTPMail-server	# checks if another job is queued
	* ? sleep 90;								    [ -f `echo $STOREDIRS | cut -d' ' -f1`/$GOTFILE.aa ] ||		    [ -f $OUTFILE ]
	| ( echo $DEFHEADER;							    echo "Subject: reply to \"$comment\"";				    echo;								    echo "I'm sorry, but another file is present:";			    ls {`echo $STOREDIRS | tr ' ' ,`}/$GOTFILE.a?;			  ) | $SENDMAIL -oi -t

	GOTLOG =$HOME/tmp/got_it.log
	TRAP   ="$TRAP; rm -f $OUTFILE*"
	TIMEOUT=86400		# waits 24h

	FTPHOST=ftp.unipd.it	# default values for fetching file
	FTPPORT=21
	FTPUSER=anonymous
	FTPPASS=lency@dei.unipd.it
	FTPRDIR=/
	FTPFILE=readme
	FTPSIZE=700k		# the file will grow of 8/7 due to base64!!!
	FTPLOG =--verbose

	:0 B			# overwrites default value
	* ^host: \/.*
	{ FTPHOST="$MATCH" }

	:0 B
	* ^port: \/.*
	{ FTPPORT="$MATCH" }

	:0 B
	* ^user: \/.*
	{ FTPUSER="$MATCH" }

	:0 B
	* ^pass: \/.*
	{ FTPPASS="$MATCH" }

	:0 B
	* ^rdir: \/.*
	{ FTPRDIR="$MATCH" }

	:0 B
	* ^file: \/.*
	{ FTPFILE="$MATCH" }

	:0 B
	* ^size: \/.*
	{ FTPSIZE="$MATCH" }

	:0 B
	* ^logging: *(no|n|off)
	{ FTPLOG }

	:0 B
	* ^debug: *(on|yes|y|s|si|sí|sì)
	{ DEBUG=si }

	# fetchs file, appends log file if required,
	# splits the file and stores them.
	:0 fbw
	| echo "--- This is your input:"; cat -;				  [ $FTPLOG ] && echo "\n--- This is the ftpget's log:";		  ftpget --sync-mode $FTPLOG							 --user $FTPUSER,$FTPPASS						 -p $FTPPORT								 $FTPHOST								 "$FTPRDIR/$FTPFILE"							 -o $OUTFILE 2>&1 > $FTPLOGFILE;				  [ $FTPLOG ] && cat $FTPLOGFILE;					  split -b $FTPSIZE								$OUTFILE $OUTFILE.;						  a=$a8; for dir in $STOREDIRS; do					     suffix=a`echo '\0'$a`; a=`echo $a+1 | bc`;				     [ -f $OUTFILE.$suffix ] &&							mv ${DEBUG:+-v} $OUTFILE.$suffix $dir 1>&2;			  done;									  exit 0

	:0 e
	| ( echo $DEFHEADER;							    echo "Subject: error for \"$comment\"";				    echo;								    echo "\tAn error has occurred while I was downloading\n";		    echo "\t\t\"$FTPFILE\"\n\n";					    echo "\tSee logs attached.";					    cat $FTPLOGFILE $LOGFILE						  ) | $SENDMAIL -oi -t;							  unset DEBUG

	:0 bc			# sends response and _continues_
	| ( echo $DEFHEADER;							    echo "Subject: reply to \"$comment\"";				    echo;								    cat -								  ) | $SENDMAIL -oi -t
   }

   :0 H
   * $^Subject: +${NEXTPHRASE} \/.*
   {
      FTPFILE="$MATCH"

      :0 fbBw
      * ^remove: \/.*
      | rm -f "$MATCH"
   }

   :0
   | a=$a8; for dir in $STOREDIRS; do						suffix=a`echo '\0'$a`; a=`echo $a+1 | bc`;				[ -f $dir/$GOTFILE.$suffix ] &&						   F2SEND=$dir/$GOTFILE.$suffix;				     done;								     metasend -b								      -z								      -e "base64"							      -m "application/octet-stream"					      -S 2000000							      -F "FTPMail-server <$LOGNAME@$HOST>"				      -t "FTPMail-client <$TO>"						      -s "new part of file: $FTPFILE"					      -D "part labeled as $F2SEND"					      -f "$F2SEND"
}
    

Lato client

LOGFILE=/home/lolo/tmp/FTPMail.log
VERBOSE=yes

:0
* !^Subject:.*Re:
* !^X-Loop: $LOGNAME@$HOST
* ^To: +FTPMail-client
* ^Subject:  new part of file: \/.*
{
   FTPFILE="$MATCH"
   origMAILDIR=$MAILDIR
   MAILDIR=$HOME/.FTPMail
   DECERR =decode-error
   LOGF   =log

   :0 H			# extracts part name
   * Content-description:  part labeled as \/.*
   {
      PARTFULL="$MATCH"
      SUFFIX=`echo $PARTFULL | tail --bytes=3`
      FILE=$WORKDIR/$SUFFIX
   }

   :0 fhw		# adds "X-Loop" filed to the header
   | formail -i"X-Loop: $LOGNAME@$HOST"

   :0 fbw		# tries to decode and save body of message
   | echo "Errors while decoding part \"$SUFFIX\" of \"$FTPFILE\"" > $DECERR;      uudeview -i -p . - 2> $DECERR;     mv UNKNOWN.001 $SUFFIX

   :0 fhwe		# if falls, passes througth with an error message
   | formail -I"Subject: error while decoding part \"$SUFFIX\" of \"$FTPFILE\""

   :0 E			# else
   {
      TO=`formail -rtx"To:" | tr -d ' '`

      :0 fbw		# throws away the body
      | :

      :0 c		# logs dec file and req next
      | date +"%b %d %T: part \"$SUFFIX\" of \"$FTPFILE\" decoded" >> $LOGF;        ( echo "From: FTPMail-client <$LOGNAME@$HOST>";				  echo "To: FTPMail-server <$TO>";					  echo "Subject: send me the next part of $FTPFILE";			  echo "";								  echo "remove: $PARTFULL"						) | $SENDMAIL -oi -t

      :0 fhbw		# if last, cats parts and goes with a "completed"
      * ? [ -e aa ]
      | formail -I"Subject: \"$FTPFILE\" completed"					-I'Content-';						        echo "The \"$FTPFILE\" has been succesfully downloaded.";		echo "You've got it in the $MAILDIR directory.";			echo "";								echo "--- log ---";							cat $LOGF;								cat a? > $FTPFILE && rm -f a?;						rm -f $LOGF $DECERR

      :0 E		# else stop
      | cat - > /dev/null

      MAILDIR=$origMAILDIR
   }

}
    
Lorenzo Cappelletti
Ultima modifica: 2001/1/22 alle 18:08
Scegli una sezione Home Chi sono Didattica Traduzioni Programmazione Elettronica Hamradio Download Sitemap