#!/bin/bash # Copyright 2006 John Duncan # Designed to enable PAN file attachment when invoked as the external editor. # As such, it may be distributed under the same license conditions, or # under the terms of the GNU General Public License v2.0 or higher (at your option). # http://pan.rebelbase.com/ or http://www.gnu.org/licenses/gpl.html. NAME="pan-attach" VERSION="0.0.2006.10.07.0" # set this to text, uuen, yenc, or leave it blank, as desired DEFAULT_FULL=uuen case $DEFAULT_FULL in uuen)DEFAULT_ENCODE=u;; yenc)DEFAULT_ENCODE=y;; text)DEFAULT_ENCODE=t;; *)DEFAULT_ENCODE= DEFAULT_FULL=unset;; esac BODYFILE=${1:--h} ################################################################################ # description and help display function function display_help { echo -e $NAME \\t version $VERSION \\t Copyright 2006 John Duncan echo echo $NAME is designed to enable PAN file attachment when invoked as its echo external editor. $NAME has one requirement, uuenview from the uudeview package, echo the home page of which is http://www.fpx.de/fp/Software/UUDeview/ echo echo Set PAN\'s external editor to $NAME %t. \(%t is the name echo of the temporary edit file as pan passes it to the editor.\) echo $NAME takes only that single parameter, either the file containing echo the body text as passed from PAN, or -h or --help for this text. echo echo Within the passed file, $NAME will look for two special lines of the form below: echo echo %%ENCODE=\%% echo -e \\t where \ is one of uuen, yenc or text. \(The default is $DEFAULT_FULL.\) echo -e \\t \(uuenview also encodes to base64 and quoted-printable, echo -e \\t however, scripting their headers would require additiional echo -e \\t help from PAN.\) The FIRST instance is taken, others are ignored. echo echo NOTE: pan \> 0.90 won\'t take yenc at the time of this writing! echo echo %%ATFILE=\%% echo -e \\t One file at a time, for now. Again, FIRST instance. echo echo Environment: echo You can set the \$PAN_EDITOR variable if you don\t want to give up your echo real external editor functionality. If $NAME finds that set and you didn\'t echo specify a file to attach, $NAME will forward the body thru to that editor, echo again as \$PAN_EDITOR %t. } ################################################################################ # This section invokes help and exits if help was directly invoked # or if the first parameter isn't an existing regular file with rw perms. [ $BODYFILE == '-h' -o $BODYFILE == '--help' ] && display_help && exit fail1="$NAME error: invalid syntax.\nThe first parameter must be a " fail2="file.\nInvoke as $NAME -h to display the help screen." [ ! -f $BODYFILE ] && echo -e $fail1 regular $fail2 && exit [ ! -r $BODYFILE ] && echo -e $fail1 readable $fail2 && exit [ ! -w $BODYFILE ] && echo -e $fail1 writable $fail2 && exit ################################################################################ # This section sets the encoding parameter, ${ENCODEAS}, to u|y|t, # and deletes the corresponding magic string line from ${BODYFILE}. # Grab %%ENCODE=text|uuen|yenc%% magic string, along with its lineno. ENCODEAS=$(egrep -anxm1 %%ENCODE=\(text\|uuen\|yenc\)%% ${BODYFILE}) if [ ${ENCODEAS} ]; then # We recognized a magic encode string line # Delete that lineno. sed -i $(echo ${ENCODEAS}|cut -d: -f1)d ${BODYFILE} # Trim the lineno. ENCODEAS=$(echo ${ENCODEAS}|cut -d: -f2) # Trim the type to single-char ENCODEAS=${ENCODEAS:9:1} else # defaulted case $DEFAULT_ENCODE in t|u|y) ENCODEAS=$DEFAULT_ENCODE;; *)echo $NAME error: no encoding line detected and no default set. Aborting! exit esac fi ################################################################################ # This section grabs the file to attach, ${ATFILE}, and deletes the # corresponding magic string line from ${BODYFILE}. # Grab %%ATFILE=/path/filename%% magic string and lineno. ATFILE=$(egrep -anxm1 %%ATFILE=.*%% ${BODYFILE}) # If no file to attach, let's assume they just wanted to edit the file... # and hope they have a sane $EDITOR set. if [ ! ${ATFILE} ]; then if [ -x "${PAN_EDITOR}" ]; then exec ${PAN_EDITOR} ${BODYFILE} else echo "Couldn't find a file to attach! If you want to pass" echo "through to an external editor of your choice, export" echo "the \$PAN_EDITOR variable, and $NAME will call it" echo "as \$PAN_EDITOR \$BODYFILE" exit 1 fi fi # We recognized a magic attach-file string line, but don't know if it's valid yet. # Still, delete that lineno. from ${BODYFILE} sed -i $(echo ${ATFILE}|cut -d: -f1)d ${BODYFILE} # Trim the lineno. ATFILE=$(echo ${ATFILE}|cut -d: -f2) # Trim to the path & file ATFILE=${ATFILE:9} ATFILE=${ATFILE%%%%} #Is ${ATFILE} actually readable? If not, error out. if [ ! -r ${ATFILE} ]; then echo "The file you said to attach," echo "${ATFILE}" echo "wasn't readable!" exit 2 fi ################################################################################ # OK, we know the attach file is readable, and we know what encoding to use, so... echo >> ${BODYFILE} echo >> ${BODYFILE} uuenview -${ENCODEAS} ${ATFILE} >> ${BODYFILE} ################################################################################ # debugging aids #konsole --noclose --nohist -e echo $ #exec kwrite $BODYFILE