Wednesday 11 May 2011

A simple utility to launch a program or script

#include <stdlib.h>
    
int main(int argc, char **argv)
{
        char prog[1024];
        strcpy(prog, argv[0]);
        strcat(prog, ".cmd");
        system(prog);
        return(0);
}

 

Launches a .cmd file with the same name as the program.  useful for PortableApps suite that will only launch executables not scripts (as far as i can tell).

Compile using tcc for example then rename to whatever you want to appear in the PortableApps menu.  now create a cmd file with the same name.  For instance if you name the launcher x.exe then the command file should be x.exe.cmd.

Put whatever you want in the command script.  Typically you would launch another executable from a relative path.

 

Posted via email from kwhitefoot's posterous

Crop an image to minimum size needed inn .NET

    public static Image Crop(Image p)
    {
        dynamic bitmap = new Bitmap(p);
        dynamic leftMost = int.MaxValue;
        dynamic rightMost = 0;
        dynamic bottomMost = int.MaxValue;
        dynamic topMost = 0;
        dynamic backgroundColour = bitmap.GetPixel(0, 0); // assumes that the background is uniform and that the real picture does not extend to the corner
        // Now search for non background pixels
        for (y = 0; y <= bitmap.Height - 1; y++) {
            for (x = 0; x <= bitmap.Width - 1; x++) {
                dynamic colour = bitmap.GetPixel(x, y);
                //Get the color of the pixel
                if (!colour.Equals(backgroundColour)) {
                    leftMost = Math.Min(leftMost, x);
                    topMost = Math.Max(topMost, y);
                    rightMost = Math.Max(rightMost, x);
                    bottomMost = Math.Min(bottomMost, y);
                }
            }
        }

        dynamic newWidth = rightMost - leftMost;
        dynamic newHeight = topMost - bottomMost;
        dynamic border = 5; // so image won't be too close to edge
        Bitmap imgCropped = new Bitmap(newWidth + 2 * border, newHeight + 2 * border);
        Graphics objGraphics = Graphics.FromImage(imgCropped);
        //set the background color to the same as the original
        objGraphics.Clear(backgroundColour);
        //Calculate the offset at which we must start drawing
        dynamic originTop = border - bottomMost;
        dynamic originLeft = border - leftMost;
        //Draw the original image to your new cropped sized image
        objGraphics.DrawImage(bitmap, originLeft, originTop);
        bitmap.Dispose();
        objGraphics.Dispose();
        //return the Cropped image to the caller
        return imgCropped;
    }

Posted via email from kwhitefoot's posterous

Friday 6 May 2011

Encapsulate in

sendtoposterous.code 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 to exist in a directory called
# .sendtoposterous 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.

# The .sendtoposterous directory is also used to maintain a history of
# post titles and tags as well as temporary files associated with
# building the email message.

#====================================================
# 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

Encapsulate in

sendtoposterous.code 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 to exist in a directory called
# .sendtoposterous 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.

# The .sendtoposterous directory is also used to maintain a history of
# post titles and tags as well as temporary files associated with
# building the email message.

#====================================================
# 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

Thursday 5 May 2011

Leading spaces

#! /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

Wednesday 4 May 2011

HTML, again

#! /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 directoryif [ ! -d "$configdir" ]then    if [ ! mkdir "$configdir"    then        zenity --info --title="$dlgtitle" --text="Could not create configuration directory."        exit 1    fifi# Create config fileif [ -e "$config" ]then    . "$config"    echo "loaded $config"else    echo "Not found $config"    AddrFrom=""    AddrTo=""    SMTPServer=""    SMTPUser=""    SMTPPass=""fiif [ "$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 bodyasktitleasktagsPostBody=""# 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 "$@"`fiecho "$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

Adding ability to find text files to use as body

#! /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

#! /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

Posted via email from kwhitefoot's posterous

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

Sunday 1 May 2011

Wrong port? Now on 587/TLS

sendtoposterous Download this file

587/TLS definitely works.

Posted via email from kwhitefoot's posterous

Wrong port?

Wrong port?

sendtoposterous Download this file

For Gmail smtp remember to use port 587 for TLS not 25 or 465. Port 25 did work now it doesn't and I don't know why.

Posted via email from kwhitefoot's posterous

Wrong port?

Followers