Subversion Repositories Plazes

[/] [BASH/] [BashLauncher.sh] - Rev 3

Compare with Previous | Blame | View Log

#!/bin/bash
 
# Plazes BashLauncher.sh - a  basic Plazes Launcher in a BASH shell script.
#
# Version:		0.7, October 6, 2005
# Author: 		Peter Rukavina (mailto:peter@rukavina.net)
# Copyright:		Copyright (c) 2005 by Reinvented Inc.
# License: 		http://www.fsf.org/licensing/licenses/gpl.txt GNU Public Licens
#
# This shell script is a very basic Plazes.com Launcher. It simply
# creates a 'plazes.launch' XML-RPC request and sends it to the
# Plazes.com XML-RPC server.  If that works (i.e. we registered with
# Plazes.com), then regular plazes.update and plazes.launch requests are
# sent, per the Plazes API.
#
# For additional information and documentation, see:
#
#  http://ruk.ca/wiki/BashLauncher
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
# System Requirements
#
# Otherwise, the script assumes the presence of:
# 	OpenSSL - used to create the MD5 hash of the password; see http://www.openssl.org/
#   netstat - used to get the default gateway.
#	arp - used to get the MAC address of the default gateway.
#   grep - used to rip out the heart of various strings.
#   cut - used to cut out the heart of various strings.
#   sed - used for string manipulation.
#   wc - used to count the number of characters in the XML-RPC request (to calculate Content-length)
#   cURL - used to send the XML-RPC request to the Plazes.com XML-RPC server; see http://curl.haxx.se/
 
# Include user-configurable options
 
. BashLauncherSettings.sh
 
# Where are various things found on this machine.  If the defaults don't work, try 'whereis [command]' -- 
# for example 'whereis openssl' and paste in the results here.
 
if [ $OS == "mac" ]; then
	OPENSSL="/usr/bin/openssl"
	ARP="/usr/sbin/arp"
	NETSTAT="/usr/sbin/netstat"
	GREP="/usr/bin/grep"
	SED="/usr/bin/sed"
	CUT="/usr/bin/cut"
	CURL="/usr/bin/curl"
	WC="/usr/bin/wc"
	DATE="/bin/date"
elif [ $OS == "linux" ]; then
	OPENSSL="/usr/bin/openssl"
	ARP="/sbin/arp"
	NETSTAT="/bin/netstat"
	GREP="/bin/grep"
	SED="/bin/sed"
	CUT="/bin/cut"
	CURL="/usr/bin/curl"
	WC="/usr/bin/wc"
	TAIL="/usr/bin/tail"
	DATE="/bin/date"
elif [ $OS == "zaurus" ]; then
	OPENSSL="/home/QtPalmtop/bin/openssl"
	ARP="/sbin/arp"
	NETSTAT="/bin/netstat"
	GREP="/bin/grep"
	SED="/bin/sed"
	CUT="/usr/bin/cut"
	CURL="/usr/bin/curl"
	WC="/usr/bin/wc"
	TAIL="/usr/bin/tail"
	DATE="/bin/date"
	OS="linux"		# Everything else works exactly the same on the Zaurus
fi
 
#---------------------------------
# BEGIN FUNCTION DEFINITIONS
#---------------------------------
 
EncryptPassword() {
 
	# Get the MD5 hash of string "PLAZES" plus the variable PLAZES_PASSWORD using OpenSSL.
	# Note that the '-n' is important -- otherwise the hash will include the terminating linefeed,
	# and Plazes.com won't understand it.
 
	PLAZES_TO_ENCRYPT="PLAZES$PLAZES_PASSWORD"
	PLAZES_ENCRYPTED=`echo -n $PLAZES_TO_ENCRYPT | $OPENSSL dgst -md5`
 
	if [ $DEBUG == 1 ]; then
		echo "Plaintext password: $PLAZES_PASSWORD"
		echo "Encrypted password: $PLAZES_ENCRYPTED"
	fi
}
 
GetMACAddress() {
 
	if [ $OS == "mac" ]; then
		# Get the default gateway using netstat.
 
		GATEWAY=`$NETSTAT -rn | $GREP default | $CUT -c20-35`
 
		if [ $DEBUG == 1 ]; then
			echo "Default Gateway   : $GATEWAY"
		fi
 
		# Get the MAC address of the default gateway.
 
		MACADDRESS=`$ARP -n $GATEWAY | cut -f4 -d' '`
 
		if [ $DEBUG == 1 ]; then
			echo "MAC Address       : $MACADDRESS"
		fi
	elif [ $OS == "linux" ]; then
		# Get the default gateway using netstat.
 
		GATEWAY=`$NETSTAT -rn | $GREP "^0.0.0.0" | $CUT -c17-31`
 
		if [ $DEBUG == 1 ]; then
			echo "Default Gateway   : $GATEWAY"
		fi
 
		# Get the MAC address of the default gateway (and convert to lower case, as required by the Plazes.com API)
 
		MACADDRESS=`$ARP -n $GATEWAY | $TAIL -n1 | cut -c34-50`
		MACADDRESS=`echo $MACADDRESS | $SED 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
 
		if [ $DEBUG == 1 ]; then
			echo "MAC Address       : $MACADDRESS"
		fi
	fi
 
	# Pad the single-hex parts of the MAC address with leading zero -- i.e. 0:4:5a:fd:83:f9 becomes 00:04:5a:fd:83:f9
	# This works using various bits of sed regular expression magic.
 
	MACADDRESS_CONVERTED=`echo $MACADDRESS | $SED "s/^\(.\):/0\1:/" | $SED "s/:\(.\):/:0\1:/g" | $SED "s/:\(.\):/:0\1:/g" | $SED "s/:\(.\)$/:0\1/"`
 
	if [ $DEBUG == 1 ]; then
		echo "MAC Address Conv  : $MACADDRESS_CONVERTED"
	fi
 
	# Substitute colons with numbers starting at 2 and incrementing -- 00:04:5a:fd:83:f9 becomes 0020435a4fd5836f9
	# There are probably considerably more elegant ways of doing this, but this one has the virtue of working.
 
	PLAZES_KEY=`echo $MACADDRESS_CONVERTED | $SED "s/:/2/" | $SED "s/:/3/" | $SED "s/:/4/" | $SED "s/:/5/" | $SED "s/:/6/"`
 
	if [ $DEBUG == 1 ]; then
		echo "Plazes Network Key: $PLAZES_KEY"
	fi
}
 
MakeLaunchRequest() {
 
	# Make the XML-RPC "plazes.launch" request.  We're not using any XML-RPC libraries here -- just hand-making XML the old way.
	# Any attempts to escape or encode this resulted in disaster for me; your mileage may vary.
 
	LAUNCHREQUEST="<?xml version='1.0'?>
<methodCall>
<methodName>plazes.launch</methodName>
<params><param><value>
<string>$PLAZES_USERNAME</string>
</value>
</param>
<param>
<value>
<string>$PLAZES_ENCRYPTED</string>
</value>
</param>
<param>
<value>
<string>$PLAZES_KEY</string>
</value>
</param>
<param>
<value>
<string>$PLAZES_DEVKEY</string>
</value>
</param>
</params>
</methodCall>"
 
	LAUNCHCONTENTLENGTH=`echo $LAUNCHREQUEST | $WC -c`
	LAUNCHCONTENTLENGTH=`expr $LAUNCHCONTENTLENGTH - 1`
	LAUNCHCONTENTLENGTH=`echo $LAUNCHCONTENTLENGTH | $SED "s/ //g"`
 
}
 
MakeUpdateRequest() {
 
	# Make the XML-RPC "plazes.update" request.
 
	UPDATEREQUEST="<?xml version='1.0'?>
<methodCall>
<methodName>plazes.update</methodName>
<params><param><value>
<string>$PLAZES_USERNAME</string>
</value>
</param>
<param>
<value>
<string>$PLAZES_ENCRYPTED</string>
</value>
</param>
<param>
<value>
<string>$PLAZES_KEY</string>
</value>
</param>
<param>
<value>
<string>$PLAZES_DEVKEY</string>
</value>
</param>
</params>
</methodCall>"
 
	UPDATECONTENTLENGTH=`echo $UPDATEREQUEST | $WC -c`
	UPDATECONTENTLENGTH=`expr $UPDATECONTENTLENGTH - 1`
	UPDATECONTENTLENGTH=`echo $UPDATECONTENTLENGTH | $SED "s/ //g"`
 
}
 
SendLaunchRequest() {
 
	# Make an HTTP POST of the XML-RPC the request, using cURL, to the Plazes.com server.
	# The XML-RPC Specification is useful to understand what's needed here -- see http://www.xmlrpc.com/spec
	#
	# The following HTTP headers are sent:
	#    Content-type: text/xml
	#    Content-length: [actual length in bytes]
	#    User-Agent: BashLauncher 0.1
	#
	# The '-s' flag makes cURL run silently (i.e. doesn't display a progress indicator).
	#
	# What gets displayed as a result of this POST is the XML-RPC response from Plazes.com -- ideally
	# (i.e. if everything worked) this will be information about the current Plaze; otherwise it will
	# be a [possibly] helpful error message.
 
	CURLREQUEST="`$CURL -s -d \"$LAUNCHREQUEST\" -H 'Content-Type: text/xml' -H \"Content-length: $LAUNCHCONTENTLENGTH\" -H 'User-Agent: BashLauncher 0.4' http://$PLAZES_SERVER$PLAZES_URI | grep start.php`"
 
	if [ X${CURLREQUEST} == "X" ]; then
		echo "XML-RPC Status    : Failed registration with $PLAZES_SERVER"
		LogSomething "plazes.launch - failed registration with $PLAZES_SERVER"
		LogSomething "Terminated process."
		exit
	else
		if [ $DEBUG == 1 ]; then
			echo "XML-RPC Status    : Successfully registered with $PLAZES_SERVER"
			LogSomething "plazes.launch - registered with $PLAZES_SERVER"
		fi
	fi
 
}
 
SendUpdateRequest() {
 
	CURLREQUEST="`$CURL -s -d \"$UPDATEREQUEST\" -H 'Content-Type: text/xml' -H \"Content-length: $UPDATECONTENTLENGTH\" -H 'User-Agent: BashLauncher 0.4' http://$PLAZES_SERVER$PLAZES_URI | grep messages`"
	if [ X${CURLREQUEST} == "X" ]; then
		echo "XML-RPC Status    : Failed update with $PLAZES_SERVER"
		LogSomething "plazes.update - failed update with $PLAZES_SERVER"
		LogSomething "Terminated process."
		exit
	else
		if [ $DEBUG == 1 ]; then
			echo "XML-RPC Status    : Successfully updated $PLAZES_SERVER"
			LogSomething "plazes.update - updated $PLAZES_SERVER"
		fi
	fi
 
}
 
LogSomething() {
 
	if [ $LOG == 1 ]; then
		NOW=`$DATE "+%b %d %H:%M:%S"`
		echo $NOW $1 >> $LOGFILE
	fi
}
 
#--------------------------
# AND HERE WE GO...
#--------------------------
 
LogSomething "BashLauncher.sh started"
EncryptPassword		# Encrypt the Plazes passwrd
GetMACAddress		# Get the current MAC address
 
# Make an initial plazes.launch request
 
MakeLaunchRequest	# Make a plazes.launch XML-RPC request
SendLaunchRequest	# Send the plazes.launch XML-RPC request
 
# It looks like Plazes.com doesn't register you as "somewhere" until you make an initial plazes.update request
 
MakeUpdateRequest	# Make a plazes.update XML-RPC request
SendUpdateRequest	# Send the plazes.update XML-RPC request
 
# An endless loop (Control + C to end) that sends 'plazes.update' and 'plazes.launch'
# XML-RPC requests at regular intervals.  The Plazes API spec says to update once every
# 4 minutes (240 seconds) and re-launch every 8 minutes (480 seconds).
 
while :
do
	if [ $DEBUG == 1 ]; then
		echo "Sleeping          : Waiting $PLAZES_BETWEEN_UPDATES seconds until update (running for $SECONDS seconds total)..."
	fi
	sleep $PLAZES_BETWEEN_UPDATES
	MakeUpdateRequest	# Make a plazes.update XML-RPC request
	SendUpdateRequest	# Send the plazes.update XML-RPC request
 
	if [ $DEBUG == 1 ]; then
		echo "Sleeping          : Waiting $PLAZES_BETWEEN_UPDATES seconds until relaunch (running for $SECONDS seconds total)..."
	fi
	sleep $PLAZES_BETWEEN_UPDATES
	MakeLaunchRequest	# Make a plazes.launch XML-RPC request
	SendLaunchRequest	# Send the plazes.launch XML-RPC request
 
done
 
#--------------------------
# THE END.
#--------------------------
 

Compare with Previous | Blame | View Log