Wednesday, 4 May 2011

Adding ability to find text files to use as body

sendtoposterous Download this file

#! /bin/bash

#===============================================
# License: GPL3 any version.

# Copyright: Kevin Whitefoot 2011

# Contact: kwhitefoot@hotmail.com

# Purpose: a script intended to be executed from file managers such as
# Nautilus to send files to a Posterous blog.

# Dependencies:
# Zenity
# sendEmail # Perl, # Net::SSLeay and IO::Socket::SSL perl modules

# Description: # This script accepts a number of file names as arguments and expects
# a configuration file called .sendtoposterous to exist in the user's
# home directory. If the configuration file does not exist then
# Zenity is used to ask the user for the SMTP server name, user name,
# password and recipient name (preset to post@posterous.com but
# presumably others could implement a similar blog submission
# process). # Once the configuration data has been collected from the user it is
# saved and the user will not be asked again unless the
# -c/--configure option is given on the command line.


#====================================================
# To do:

# - Reliable detection of filure to send.

# - Use file as body. Use file command to discover text files,
# present list so user can pick one to use as the body.
#====================================================
# $Id: sendtoposterous,v 1.10 2011/05/02 19:33:05 kj Exp $
#
# $Log: sendtoposterous,v $
# Revision 1.10 2011/05/02 19:33:05 kj
# Fixed tags file so that only the tags need to be present. Uses sed to add boolean False before each line.
#
# Revision 1.9 2011/05/02 19:21:37 kj
# Tags history now works.
#
# Revision 1.8 2011/05/02 17:52:00 kj
# Added title history and tags.
#
# Revision 1.7 2011/04/29 16:12:12 kj
# Changed from a single config file to a config directory so that we can store history.
#
# Revision 1.6 2011/04/27 20:41:40 kj
# Added post title as text on progress dialog.
#
# Revision 1.5 2011/04/27 19:41:51 kj
# Removed grep filtering of progress, pulsating bar works now.
#
# Revision 1.4 2011/04/27 19:36:26 kj
# .
#

#====================================================

set -x # Uncomment for debugging

echo "Starting $0"


# Search the list of files for files that can be used as the body of
# the file.
findtextfiles(){
for f in "$@"
do
if file "$f" | grep text
then
textfiles=("${textfiles[@]}" "False")
textfiles=("${textfiles[@]}" "$f")
fi
done

}

# Note that you cannot use echo to output debugging information inside
# this function because it is the output on stdout that the caller
# uses.
ask() {
zenity --entry --title="dlgtitle" --text="$1" --entry-text="$2"
if [ $? != 0 ]
then
echo "User cancelled"
set -e # Ensure that the exit in the next line exits the whole script.
exit 1
fi }

# Use a function to send so that we can start a Zenity notification
# icon or progress bar.
send(){
subject="$PostTitle ((tags: $PostTags))"
sendEmail -v -f "$AddrFrom" -t "$AddrTo" -u "$subject" -m "$PostBody" -s "$SMTPServer" -xu "$SMTPUser" -xp "$SMTPPass" -a "$@" | tee >(zenity --progress --pulsate --auto-close --title="$dlgtitle" --text="$PostTitle")
} sendfilebody(){
subject="$PostTitle ((tags: $PostTags))"
cat "$tmpbody" | sendEmail -v -f "$AddrFrom" -t "$AddrTo" -u "$subject" -s "$SMTPServer" -xu "$SMTPUser" -xp "$SMTPPass" -a "$@" | tee >(zenity --progress --pulsate --auto-close --title="$dlgtitle" --text="$PostTitle")
}


# Ask the user for a title, present the history of titles so that the
# user can pick.
asktitle(){

# First, ensure that the titles file has at least one line in it.
if [[ ! -s "$titlesfile" ]] then
echo "Posted by sendtoposterous on `date`" > "$titlesfile" fi
PostTitle=`cat "$titlesfile" | zenity --list \
--editable \
--title="$dlgtitle" \
--column="Title (click or enter to edit)"`
if [ "$PostTitle" = "" ]
then
exit 1
fi
# Add the selected title to a file of titles. Add to the start of
# the file. Remove duplicates so that even if the user re-uses a
# title it will only appear in the file once.
addtohistory "$PostTitle" "$titlesfile"
}

# Ask the user for the tags. User can pick as many as required.
asktags(){

# First, ensure that the tags file has at least one tag in it so
# that we can easily detect whether there are any..
if [[ ! -s "$tagsfile" ]] then
echo "`date`" >> "$tagsfile" fi
# Add the boolean column to the tag file and pipe it to zenity.
PostTags=`sed '/.*/i False' $tagsfile | zenity --list \
--title="$dlgtitle" \
--editable \
--checklist \
--column="Select" \
--column="Tag (click or enter to edit)" \
--separator=", "`
if [ "$PostTags" = "" ]
then # user cancelled
exit 1
fi
# Present the tag to the user as a comma separated list for
# further editing and confirmation.
PostTags=`zenity --entry \
--title="$dlgtitle" \
--text="Edit and confirm tags" \
--entry-text="$PostTags"`
# Add the selected tags to a file of tags. Add to the start of
# the file. Remove duplicates so that even if the user re-uses a
# title it will only appear in the file once.
tags=(${PostTags//,/ }) replace commas with spaces

# Back up the tags file
mv "$tagsfile" "${tagsfile}.bak"
# Write the new tags to the, now empty, tag file. Note that we do
# not need the booleans, just the tags, one per line.
for tag in "${tags[@]}"
do
echo "$tag" >> "$tagsfile"
done
# Now remove the selected tags from the backup
for tag in "${tags[@]}"
do
sed -in-place "/$tag/d" "${tagsfile}.bak"
done
# Finally append the history to the new tags
cat "${tagsfile}.bak" >> "$tagsfile"
}

# Add an entry to a history file, remove duplicates, place new item at
# the top.
addtohistory(){
item="$1"
file="$2"
sed "/$item/d" "${file}" > "${file}.bak"
echo "$item" > "${file}"
cat "${file}.bak" >> "${file}"
}


# Initialize configdir="$HOME/.`basename $0`"
config="$configdir/config"
echo "looking for config file: $config"
titlesfile="$configdir/titles"
tagsfile="$configdir/tags"
tmpbody="$configdir/tmpbody"
declare -a textfiles=()
dlgtitle="Send to Posterous"

# Create config directory
if [ ! -d "$configdir" ]
then
if [ ! mkdir "$configdir"
then
zenity --info --title="$dlgtitle" --text="Could not create configuration directory."
exit 1
fi
fi

# Create config file
if [ -e "$config" ]
then
. "$config"
echo "loaded $config"
else
echo "Not found $config"
AddrFrom=""
AddrTo=""
SMTPServer=""
SMTPUser=""
SMTPPass=""
fi


if [ "$AddrFrom" = "" -o "$AddrTo" = "" -o "$SMTPServer" = "" -o "$SMTPUser" = "" -o "$SMTPPass" = "" ]
then

# Set defaults
if [ "$AddrTo" = "" ]
then AddrTo="post@posterous.com"
fi
if [ "$SMTPServer" = "" ]
then SMTPServer="smtp.gmail.com"
fi

echo "Ask user for config"
SMTPServer=`ask "SMTP server" "$SMTPServer"`
SMTPUser=`ask "SMTP user" "$SMTPUser"`
SMTPPass=`ask "SMTP password" "$SMTPPass"`
AddrFrom=`ask "From" "$AddrFrom"`
AddrTo=`ask "To" "$AddrTo"`

# Save config
echo "SMTPServer=$SMTPServer" > "$config"
echo "SMTPUser=$SMTPUser" >> "$config"
echo "SMTPPass=$SMTPPass" >> "$config"
echo "AddrFrom=$AddrFrom" >> "$config"
echo "AddrTo=$AddrTo" >> "$config"
else
echo "Got config"
fi

# Now ask the user for the title and body
asktitle
asktags

PostBody=""
# Always add a space to the body to ensure that sendEmail doesn't
# think it needs to ask again.
findtextfiles "$@"
echo "debug text files: ${textfiles[@]}" if [ "${#textfiles[@]}" -ne "0" ]
then
bodyfiles=`zenity --title="$dlgtitle" --text="Pick one or more files." --checklist --list --column="select" --column="file" "${textfiles[@]}"` fi

# Now we have everything we can construct the command line except the
# body. Show a progress bar so the user knows we are trying even
# though we cannot report progress.
echo "text files: ${textfiles[@]}" echo "body files: ${bodyfiles[@]}"
if [ "${#bodyfiles[@]}" -ne "0" ]
then
cat "${bodyfiles[@]}" > "$tmpbody"
sendresult=`sendfilebody "$@"`
else
PostBody=`ask "Body" "$PostBody"`" "
sendresult=`send "$@"`
fi

echo "$sendresult"

if [ $? = 0 ]
then
zenity --info --title="$dlgtitle" --text="$PostTitle\rSuccess."
else
zenity --warning --title="$dlgtitle" --text="Failed\r$sendresult"
fi

Posted via email from kwhitefoot's posterous

No comments:

Post a Comment

Followers